def paddingUI(): padding = self.padding.value if padding is None: def onPadUniform(button, event): self.padding.value = UniformPadding() def onPadNonUniform(button, event): self.padding.value = NonUniformPadding() return ControlsRow([ Button.buttonWithLabel('Uniform', onPadUniform), Button.buttonWithLabel('Non-uniform', onPadNonUniform) ]).alignHPack() else: def onRemove(button, event): self.padding.value = None def setPadding(padding): self.padding.value = padding removeButton = Button.buttonWithLabel('Remove padding', onRemove).alignHPack() return Column([ removeButton.alignHPack(), Spacer(0.0, 5.0), padding.editUI(setPadding) ])
def editUI(self, controlFactoryFn): self.__incr.onAccess() valueControl = controlFactoryFn(self._live) if self._expr is None: def _onAdd(button, event): self.expr = EmbeddedPython2Expr() addButton = Button(self._addButtonContents, _onAdd).alignHPack() return Row([valueControl, Spacer(10.0, 0.0), addButton]) else: def _onRemove(button, event): self.expr = None removeButton = Button(self._removeButtonContents, _onRemove).alignHPack() return Column([ valueControl, Row([ removeButton, Spacer(10.0, 0.0), exprBorder.surround(self._expr) ]) ])
def __present_contents__(self, fragment, inheritedState): def _onApply(button, event): self._config = self._userConfig.getStaticValue() self.apply() def _onRevert(button, event): self._userConfig.setLiteralValue(self._config) choices = [Label(c[1]) for c in _fontConfigChoices] choices.append(Label('Custom')) @LiveFunction def indexOfChoice(): config = self._userConfig.getValue() if isinstance(config, FontConfiguration): return len(choices) - 1 else: try: return [c[0] for c in _fontConfigChoices ].index(self._userConfig.getValue()) except ValueError: return 0 @LiveFunction def footer(): config = self._userConfig.getValue() if isinstance(config, FontConfiguration): return config else: return Blank() def _onChoice(menu, prevChoice, choice): if choice == len(choices) - 1: # Custom if prevChoice != choice: prevConfig = _fontConfigChoices[prevChoice][0] prevConfig = _fontConfigForName(prevConfig) config = FontConfiguration() config.copyFrom(prevConfig) self._userConfig.setLiteralValue(config) else: self._userConfig.setLiteralValue(_fontConfigChoices[choice][0]) applyButton = Button.buttonWithLabel('Apply', _onApply) revertButton = Button.buttonWithLabel('Revert', _onRevert) buttons = Row([applyButton, Spacer(15.0, 0.0), revertButton]).alignHPack() configMenu = OptionMenu(choices, indexOfChoice, _onChoice) chooserHeader = Row([ Label('Choose a font configuration:'), Spacer(25.0, 0.0), configMenu ]).alignHPack() return self._pageColumnStyle(Column([buttons, chooserHeader, footer]))
def _addPanelButtons(sourceElement, menu): # Side panel def _onShow(button, event): showSidePanel(sourceElement.rootElement) def _onHide(button, event): hideSidePanel(sourceElement.rootElement) panelButtons = ControlsRow([Button.buttonWithLabel('Show', _onShow), Button.buttonWithLabel('Hide', _onHide)]) panelSection = Section(SectionHeading3('Side panel'), panelButtons) menu.add(panelSection)
def _documentContextMenuFactory(element, menu): region = element.getRegion() rootElement = element.getRootElement() def makeParagraphStyleFn(style): def setParagraphStyle(model): model.style = style def _onLink(link, event): caret = rootElement.getCaret() if caret is not None and caret.isValid(): caretElement = caret.getElement() if caretElement.getRegion() is region: GUIRichTextController.instance.modifyParagraphAtMarker(caret.getMarker(), setParagraphStyle) return _onLink normalStyle = Hyperlink('Normal', makeParagraphStyleFn('normal')) h1Style = Hyperlink('H1', makeParagraphStyleFn('h1')) h2Style = Hyperlink('H2', makeParagraphStyleFn('h2')) h3Style = Hyperlink('H3', makeParagraphStyleFn('h3')) h4Style = Hyperlink('H4', makeParagraphStyleFn('h4')) h5Style = Hyperlink('H5', makeParagraphStyleFn('h5')) h6Style = Hyperlink('H6', makeParagraphStyleFn('h6')) titleStyle = Hyperlink('Title', makeParagraphStyleFn('title')) paraStyles = ControlsRow([normalStyle, h1Style, h2Style, h3Style, h4Style, h5Style, h6Style, titleStyle]) menu.add(Section(SectionHeading2('Paragraph styles'), paraStyles)) def makeStyleFn(attrName): def computeStyleValues(listOfSpanAttrs): value = bool(listOfSpanAttrs[0].getValue(attrName, 0)) value = not value attrs = RichTextAttributes() attrs.putOverride(attrName, '1' if value else None) return attrs def onButton(button, event): selection = rootElement.getSelection() if isinstance(selection, TextSelection): if selection.getRegion() == region: GUIRichTextController.instance.applyStyleToSelection(selection, computeStyleValues) return onButton italicStyle = Button.buttonWithLabel('I', makeStyleFn('italic')) boldStyle = Button.buttonWithLabel('B', makeStyleFn('bold')) styles = ControlsRow([italicStyle, boldStyle]).alignHLeft() menu.add(Section(SectionHeading2('Selection styles'), styles)) return True
def editor(): x = live.getValue() if x is None: def on_add(button, event): live.setLiteralValue(initialValue) return Button(_plusStyle(Label('+')), on_add).alignHPack() else: def on_delete(button, event): live.setLiteralValue(None) deleteButton = Button(Image.systemIcon('delete'), on_delete) return Row([deleteButton.alignHPack(), Spacer(5.0, 0.0).alignHPack(), valueEditor])
def __present__(self, fragment, inheritedState): def _onClick(button, event): self._stepper._onRun() return Button( Row([_forwardArrow, Spacer(2.0, 0.0), _forwardArrow]).pad(2.0, 2.0), _onClick).alignVCentre()
def _testValue(self, kind, data, excType=None): self._actual.setLiteralValue( ( kind, data ) ) expected = self._expected.getStaticValue() if expected is not None: expectedKind, expectedData = expected if kind == expectedKind: if expectedKind == 'exception': if excType == expectedData: self.__result.setLiteralValue( _resultPass ) else: title = _resultFailStyle( Label( 'FAIL' ) ) heading = Label( 'Expected exception of type %s, got:' % expectedData.__name__ ) res = Column( [ title, heading, data ] ) self.__result.setLiteralValue( res ) elif expectedKind == 'value': if data == expectedData: self.__result.setLiteralValue( _resultPass ) else: title = _resultFailStyle( Label( 'FAIL' ) ) heading1 = Label( 'Expected:' ) heading2 = Label( 'Got:' ) res = Column( [ title, heading1, expectedData, heading2, data ] ) self.__result.setLiteralValue( res ) else: raise TypeError, 'unknown expected result kind %s' % expectedKind else: resContents = [ _resultFailStyle( Label( 'FAIL' ) ) ] if expectedKind == 'exception': resContents.append( Label( 'Expected exception of type %s:' % expectedData.__name__ ) ) elif expectedKind == 'value': resContents.append( Label( 'Expected:' ) ) resContents.append( expectedData ) else: raise TypeError, 'unknown expected result kind %s' % expectedKind if kind == 'exception': resContents.append( Label( 'Got exception:' ) ) resContents.append( data ) elif kind == 'value': resContents.append( Label( 'Expected:' ) ) resContents.append( data ) else: raise TypeError, 'unknown result kind %s' % kind self.__result.setLiteralValue( Column( resContents ) ) else: def _onFix(button, event): if kind == 'exception': self._expected.setLiteralValue( ( kind, excType ) ) elif kind == 'value': self._expected.setLiteralValue( ( kind, data ) ) else: raise TypeError, 'unknown result kind %s' % kind self.__result.setLiteralValue( _resultNone ) fixButton = Button.buttonWithLabel( 'Set expected result', _onFix ) title = Label( 'No expected result, received:' ) self.__result.setLiteralValue( Column( [ title, Pres.coerce( data ).pad( 5.0, 0.0, 5.0, 5.0 ), fixButton ] ).alignHPack().alignVTop() )
def _worksheetContextMenuFactory(element, menu): def _onRefresh(button, event): model.refreshResults() model = element.getFragmentContext().getModel() refreshButton = Button.buttonWithLabel('Refresh', _onRefresh) worksheetControls = ControlsRow([refreshButton.alignHPack()]) menu.add(Section(SectionHeading2('Worksheet'), worksheetControls)) return True
def __present__(self, fragment, inheritedState): def _literateExpressionMenu(element, menu): def _onToggleExpanded(item): self.setExpanded(not self._expanded) menuItemName = 'Start as contracted' if self._expanded else 'Start as expanded' menu.add( MenuItem.menuItemWithLabel(menuItemName, _onToggleExpanded)) return False self._incr.onAccess() self._definition._incr.onAccess() suitePres = self._definition._suite nameLabel = self._nameStyle(Label(self._definition._name)) liveName = LiveValue(nameLabel) class _NameEntryListener(TextEntry.TextEntryListener): def onAccept(listener, textEntry, text): self.setName(text) def onCancel(listener, textEntry, orignalText): liveName.setLiteralValue(nameLabel) def _onNameButton(button, event): nameEntry = TextEntry(self._definition._name, _NameEntryListener()) nameEntry.grabCaretOnRealise() nameEntry = self._nameStyle(nameEntry) liveName.setLiteralValue(nameEntry) renameButton = self._nameButtonStyle( Button.buttonWithLabel('...', _onNameButton)) header = Row([ self._angleQuoteStyle(Label(u'\u00ab')), liveName, self._angleQuoteStyle(Label(u'\u00bb')), Spacer(10.0, 0.0), renameButton ]).withDragSource(_dragSource) dropDown = self._dropDownStyle( DropDownExpander(header, suitePres, self._expanded, None)) return ObjectBorder(dropDown).withContextMenuInteractor( _literateExpressionMenu)
def buildEditorPres(self, fragment, inheritedState): def _onOpen(button, event): component = button.getElement().getRootElement().getComponent() fileChooser = JFileChooser() self._initFileChooser(fileChooser) response = fileChooser.showDialog(component, 'Open') if response == JFileChooser.APPROVE_OPTION: sf = fileChooser.getSelectedFile() if sf is not None: filename = sf.getPath() if filename is not None: self._model.liveValue.setLiteralValue(filename) openButton = Button.buttonWithLabel('...', _onOpen) return Row([ openButton, Spacer(3.0, 0.0), EditableLabel(self._model.liveValue, _notSetStyle(Label('<none>'))).regexValidated( _filenamePattern, 'Please enter a valid filename') ])
def samplePage(): label = Label('Sample page:') title = titleSample(self._title.getValue(), 'Example Page Title') heading = headingSample(self._heading.getValue(), 'Main heading') normal1 = normalSample(self._normal.getValue(), 'Normal text will appear like this.') normal2 = normalSample( self._normal.getValue(), 'Paragraphs of normal text are used for standard content.') ui1 = uiHeadingSample(self._uiHeading.getValue(), 'UI heading') genericLabel = Label( 'Generic text (within controls, code, etc) will appear like this.' ) buttons = self._buttonRowStyle( Row([ Button.buttonWithLabel('Button %d' % (i, ), None) for i in xrange(0, 5) ])) ui = Section(ui1, Column([genericLabel, Spacer(0.0, 7.0), buttons])) page = Page([title, Body([heading, normal1, normal2, ui])]) return Column([label, Spacer(0.0, 15.0), page])
def _presentBranchContents(self, child, fragment, inheritedState): return Button(child, None)
def ProjectRoot(self, fragment, inheritedState, project): # Save and Save As def _onSave(control, buttonEvent): if document.hasFilename(): document.save() else: def handleSaveDocumentAsFn(filename): document.saveAs(filename) DocumentManagement.promptSaveDocumentAs( world, control.getElement().getRootElement().getComponent(), handleSaveDocumentAsFn) def _onSaveAs(control, buttonEvent): def handleSaveDocumentAsFn(filename): document.saveAs(filename) DocumentManagement.promptSaveDocumentAs( world, control.getElement().getRootElement().getComponent(), handleSaveDocumentAsFn, document.getFilename()) def _onReload(control, buttonEvent): if document.hasFilename(): document.save() document.reload() project.reset() else: def handleSaveDocumentAsFn(filename): document.saveAs(filename) document.reload() project.reset() DocumentManagement.promptSaveDocumentAs( world, control.getElement().getRootElement().getComponent(), handleSaveDocumentAsFn) def _onExport(control, event): component = control.getElement().getRootElement().getComponent() openDialog = JFileChooser() openDialog.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY) response = openDialog.showDialog(component, 'Export') if response == JFileChooser.APPROVE_OPTION: sf = openDialog.getSelectedFile() if sf is not None: filename = sf.getPath() if filename is not None and os.path.isdir(filename): response = JOptionPane.showOptionDialog( component, 'Existing content will be overwritten. Proceed?', 'Overwrite existing content', JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, None, ['Overwrite', 'Cancel'], 'Cancel') if response == JFileChooser.APPROVE_OPTION: exc = None try: project.export(filename) except: exc = JythonException.getCurrentException() if exc is not None: BubblePopup.popupInBubbleAdjacentTo( DefaultPerspective.instance(exc), control.getElement(), Anchor.BOTTOM, True, True) # Python package name class _PythonPackageNameListener(EditableLabel.EditableLabelListener): def onTextChanged(self, editableLabel, text): if text != '': project.pythonPackageName = text else: project.pythonPackageName = None # Project index def _addPackage(menuItem): project.append(ProjectPackage('NewPackage')) def _addPage(page): project.append(page) def _projectIndexContextMenuFactory(element, menu): menu.add(MenuItem.menuItemWithLabel('New package', _addPackage)) newPageMenu = PageData.newPageMenu(_addPage) importPageMenu = PageData.importPageMenu( element.getRootElement().getComponent(), _addPage) menu.add( MenuItem.menuItemWithLabel( 'New page', newPageMenu, MenuItem.SubmenuPopupDirection.RIGHT)) menu.add( MenuItem.menuItemWithLabel( 'Import page', importPageMenu, MenuItem.SubmenuPopupDirection.RIGHT)) return True # Get some initial variables document = fragment.subject.document world = fragment.subject.world # Title title = TitleBar(document.getDocumentName()) # Controls for 'save' and 'save as' saveExportHeader = SectionHeading1('Save/export') saveButton = Button.buttonWithLabel('Save', _onSave) saveAsButton = Button.buttonWithLabel('Save as', _onSaveAs) reloadButton = Button.buttonWithLabel('Save and reload', _onReload) reloadButton = AttachTooltip( reloadButton, 'Saves and reloads the project from scratch\nCauses all embedded objects to be re-created.' ) exportButton = Button.buttonWithLabel('Export', _onExport) exportButton = AttachTooltip( exportButton, 'Exports project contents to text files where possible.') saveBox = Row([ saveButton.padX(10.0), Spacer(30.0, 0.0), saveAsButton.padX(10.0), Spacer(30.0, 0.0), reloadButton.padX(10.0), Spacer(50.0, 0.0), exportButton.padX(10.0) ]).alignHLeft() saveExportSection = Section(saveExportHeader, saveBox) # # Project Section # # Python package name notSet = _pythonPackageNameNotSetStyle.applyTo(Label('<not set>')) pythonPackageNameLabel = EditableLabel( project.pythonPackageName, notSet, _PythonPackageNameListener()).regexValidated( _pythonPackageNameRegex, 'Please enter a valid dotted identifier') pythonPackageNameLabel = AttachTooltip( pythonPackageNameLabel, 'The root python package name is the name under which the contents of the project can be imported using import statements within the project.\n' + \ 'If this is not set, pages from this project cannot be imported.', False ) pythonPackageNameRow = Form.Section( 'Root Python package name', 'Pages will not be importable unless this is set', pythonPackageNameLabel) # Clear imported modules def _onReset(button, event): project.reset() modules = document.unloadAllImportedModules() heading = SectionHeading2('Unloaded modules:') modules = Column([Label(module) for module in modules]) report = Section(heading, modules) BubblePopup.popupInBubbleAdjacentTo(report, button.getElement(), Anchor.BOTTOM, True, True) resetButton = Button.buttonWithLabel('Reset', _onReset) resetButton = AttachTooltip( resetButton, 'Unloads all modules that were imported from this project from the Python module cache. This way they can be re-imported, allowing modifications to take effect.' ) resetRow = Form.Section('Reset', 'Unload project modules', resetButton) projectSection = Form('Project', [pythonPackageNameRow, resetRow]) # Project index indexHeader = SectionHeading1('Index') nameElement = _projectIndexNameStyle.applyTo(Label('Project root')) nameBox = _itemHoverHighlightStyle.applyTo(nameElement.alignVCentre()) nameBox = nameBox.withContextMenuInteractor( _projectIndexContextMenuFactory) nameBox = _ProjectTreeController.instance.item(project, nameBox) nameBox = AttachTooltip( nameBox, 'Right click to access context menu, from which new pages and packages can be created.\n' + \ 'A page called index at the root will appear instead of the project page. A page called __startup__ will be executed at start time.', False ) itemsBox = Column(project[:]).alignHExpand() itemsBox = _ProjectTreeController.instance.editableList( project, itemsBox) contentsView = Column([ nameBox.alignHExpand(), itemsBox.padX(_packageContentsIndentation, 0.0).alignHExpand() ]) indexSection = Section(indexHeader, contentsView) def _onBuildJar(button, event): _buildProjectJar(button.element, document) buildJarButton = Button.buttonWithLabel('Build JAR', _onBuildJar) jarRow = Form.Section('Build executable app', 'Export the project as an executable JAR', buildJarButton) packagingSection = Form('Packaging', [jarRow]) indexTip = TipBox([ NormalText([ StrongSpan('Index: '), 'Larch projects act like Python programs. Packages act as directories/packages and pages act as Python source files. Pages can import code from one another as if they are modules.' ]), NormalText([ 'New pages and packages can be created by right clicking on the entries in the index or on ', EmphSpan('Project root'), ' (they will highlight as you hover over them).' ]), NormalText([ StrongSpan('Front and startup pages: '), 'If a page is set as the front page it will appear instead of the project page. In these cases, the project page can still be reached using the links in the location bar at the top of the window.' ]), 'If a page is set as the startup page, code within it will be executed before all other pages. This can be used for registering editor extensions.', 'To set a page as the front page or the startup page, right-click on it to show its context menu and choose the appropriate option.' ], 'larchcore.worksheet.worksheeteditor') # The page head = Head([title]) body = Body([ saveExportSection, projectSection, indexSection, packagingSection, indexTip ]).alignHPack() return StyleSheet.style(Primitive.editable(False)).applyTo( Page([head, body]))
def _documentContextMenuFactory(element, menu): region = element.getRegion() rootElement = element.getRootElement() def makeParagraphStyleFn(style): def setStyle(model): model.setStyle(style) def _onLink(link, event): caret = rootElement.getCaret() if caret is not None and caret.isValid(): caretElement = caret.getElement() if caretElement.getRegion() is region: _controller.modifyParagraphAtMarker(caret.getMarker(), setStyle) return _onLink def insertEmbedPara(link, event): def _newEmbedPara(): img = _imageFileChooser(link.element, lambda f: _ParaImage(f)) return embed.ParaEmbed(img) if img is not None else None caret = rootElement.getCaret() if caret is not None and caret.isValid(): caretElement = caret.getElement() if caretElement.getRegion() is region: _controller.insertParagraphAtCaret(caret, _newEmbedPara) normalStyle = Hyperlink('Normal', makeParagraphStyleFn('normal')) h1Style = Hyperlink('H1', makeParagraphStyleFn('h1')) h2Style = Hyperlink('H2', makeParagraphStyleFn('h2')) h3Style = Hyperlink('H3', makeParagraphStyleFn('h3')) h4Style = Hyperlink('H4', makeParagraphStyleFn('h4')) h5Style = Hyperlink('H5', makeParagraphStyleFn('h5')) h6Style = Hyperlink('H6', makeParagraphStyleFn('h6')) titleStyle = Hyperlink('Title', makeParagraphStyleFn('title')) paraStyles = ControlsRow([normalStyle, h1Style, h2Style, h3Style, h4Style, h5Style, h6Style, titleStyle]) embedPara = Hyperlink('Embed para', insertEmbedPara) paraEmbeds = ControlsRow([embedPara]) menu.add(Section(SectionHeading2('Paragraph styles'), paraStyles)) menu.add(Section(SectionHeading2('Paragraph embeds'), paraEmbeds)) def makeStyleFn(attrName): def computeStyleValues(listOfSpanAttrs): value = listOfSpanAttrs[0].getValue(attrName, 0) value = not value attrs = RichTextAttributes() attrs.putOverride(attrName, value) return attrs def onButton(button, event): selection = rootElement.getSelection() if isinstance(selection, TextSelection): if selection.getRegion() == region: _controller.applyStyleToSelection(selection, computeStyleValues) return onButton def _onInsertInlineEmbed(button, event): def _newInlineEmbedValue(): return _imageFileChooser(button.element, lambda f: _InlineImage(f)) caret = rootElement.getCaret() if caret is not None and caret.isValid(): caretElement = caret.getElement() if caretElement.getRegion() is region: _controller.insertInlineEmbedAtMarker(caret.getMarker(), _newInlineEmbedValue) def style_button(text, on_click, *style_values): sty = StyleSheet.instance.withValues(Primitive.fontFace(Primitive.monospacedFontName), *style_values) return Button(sty.applyTo(Label(text)), on_click) italicStyle = style_button('I', makeStyleFn('i'), Primitive.fontItalic(True)) boldStyle = style_button('B', makeStyleFn('b'), Primitive.fontBold(True)) codeStyle = style_button('code', makeStyleFn('code')) cmdStyle = style_button('> cmd', makeStyleFn('cmd')) appStyle = style_button('app', makeStyleFn('app')) sysStyle = style_button('sys', makeStyleFn('sys')) styles = ControlsRow([italicStyle, boldStyle, codeStyle, cmdStyle, appStyle, sysStyle]).alignHLeft() insertInlineEmbed = Button.buttonWithLabel('Embed', _onInsertInlineEmbed) inlineEmbeds = ControlsRow([insertInlineEmbed]).alignHLeft() menu.add(Section(SectionHeading2('Selection styles'), styles)) menu.add(Section(SectionHeading2('Inline embeds'), inlineEmbeds)) return True
def style_button(text, on_click, *style_values): sty = StyleSheet.instance.withValues(Primitive.fontFace(Primitive.monospacedFontName), *style_values) return Button(sty.applyTo(Label(text)), on_click)