ProgressBar framerate drops when Label draws on top - scala

I'd not normally ask for help here, but I'm stumped - this bug is the strangest thing I've seen in a long time.
https://gfycat.com/FluidFrigidEastsiberianlaika
I've got a simple UI object called GhostProgressBar that extends ScalaFX.StackPane and gives it two children - a ProgressBar and a Label. I noticed after adding it to some other UI screens that my framerate had plummeted, to a point where the UI was painfully unusuable.
The code for this is super simple:
import scalafx.geometry.Pos
import scalafx.scene.control.{Label, ProgressBar}
import scalafx.scene.layout.StackPane
class GhostProgressBar extends StackPane {
alignment = Pos.Center
val bar = new ProgressBar() {
prefWidth = Integer.MAX_VALUE
}
val text = new Label() {
id = "ProgressBarText"
text = "PERFORMANCE TESTING"
}
children = List(bar, text)
}
In the GIF I'm using it inside a VBox that's the center element of a regular BorderPane - nothing strange or atypical.
From the behaviour I've observed, I think it's an issue with text being drawn over the bar of the ProgressBar. Just now I've done some more debugging, and my suspicions that it was related to the styling of the text were confirmed.
This is the styling that's on the text in the GIF.
#ProgressBarText {
-fx-text-fill: #dddddd;
-fx-font-weight: bold;
}
#ProgressBarText .text {
-fx-stroke: #333333;
-fx-stroke-width: 1px;
-fx-stroke-type: outside;
}
When I remove that styling, the framerate doesn't drop when the bar hits the text.
What I can't figure out is why this is happening? Anyone got any ideas? I have no idea if it's a Scala thing, or a ScalaFX thing, whether or not it's reproducible with the same stuff in a JavaFX context.
Help would be appreciated.
EDIT: I was asked for versions, here we go:
Scala version: 2.12.7
ScalaFX version: 8.0.102-R11
JDK version: 1.8.0_181
JavaFX version: unknown, I'm not familiar with ScalaFX's internals and I'm not using JavaFX directly.
EDIT 2: I was asked to try the same screen elements, but using JavaFX elements instead of ScalaFX ones. Here's the code I used, the outcome was the same - whenever the outlined text was over the progress bar's bar, the framerate dropped.
import javafx.geometry.Pos
import javafx.scene.control.{Label, ProgressBar}
import javafx.scene.layout.StackPane
class JavaFXGhostProgressBar extends StackPane {
this.setAlignment(Pos.CENTER)
val bar = new ProgressBar()
bar.setMaxWidth(Double.MaxValue)
val text = new Label()
text.textProperty().setValue("PERFORMANCE TESTING")
text.idProperty().setValue("OutlineProgressBarText")
this.getChildren.addAll(bar, text)
}
I couldn't find out what version of JavaFX I used here; IntelliJ was weirdly inconsiderate in not telling me. I couldn't find it in my external libraries list, either.

Related

How to search for and highlight a substring in Codemirror 6?

I'm building a simple code editor to help children learn HTML. One feature I'm trying to add is that when users mouseover their rendered code (in an iframe), the corresponding HTML code in the editor is highlighted. So, for example, if a user mouses-over an image of kittens, the actual code, , would be highlighted in the editor.
Mousing-over the iframe to get the html source for that element is the easy part, which I've done (using document.elementFromPoint(e.clientX, e.clientY in the iframe itself, and posting that up to the parent) - so that's not the part I need help with. The part I can't figure out is how to search for and highlight that string of selected code in the code editor.
I'm using Codemirror 6 for this project, as it seems as it will give me the most flexibility to create such a feature. However, as a Codemirror 6 novice, I'm struggling with the documentation to find out where I should start. It seems like the steps I need to complete to accomplish this are:
Search for a range in the editor's text that matches a string (ie.'<img src="kittens.gif"').
Highlight that range in the editor.
Can anyone out there give me some advice as to where in the Codemirror 6 API I should look to start implementing this? It seems like it should be easy, but my unfamiliarity with the Codemirror API and the terse documentation is making this difficult.
1. Search for a range in the editor's text that matches a string (ie.'<img src="kittens.gif"').
You can use SearchCursor class (iterator) to get the character's range where is located the DOM element in your editor.
// the import for SearchCursor class
import {SearchCursor} from "#codemirror/search"
// your editor's view
let main_view = new EditorView({ /* your code */ });
// will create a cursor based on the doc content and the DOM element as a string (outerHTML)
let cursor = new SearchCursor(main_view.state.doc, element.outerHTML);
// will search the first match of the string element.outerHTML in the editor view main_view.state.doc
cursor.next()
// display the range where is located your DOM element in your editor
console.log(cursor.value);
2. Highlight that range in the editor.
As described in the migration documentation here, marked text is replace by decoration. To highlight a range in the editor with codemirror 6, you need to create one decoration and apply it in a dispatch on your view. This decoration need to be provide by an extension that you add in the extensions of your editor view.
// the import for the 3 new classes
import {StateEffect, StateField} from "#codemirror/state"
import {Decoration} from "#codemirror/view"
// code mirror effect that you will use to define the effect you want (the decoration)
const highlight_effect = StateEffect.define();
// define a new field that will be attached to your view state as an extension, update will be called at each editor's change
const highlight_extension = StateField.define({
create() { return Decoration.none },
update(value, transaction) {
value = value.map(transaction.changes)
for (let effect of transaction.effects) {
if (effect.is(highlight_effect)) value = value.update({add: effect.value, sort: true})
}
return value
},
provide: f => EditorView.decorations.from(f)
});
// this is your decoration where you can define the change you want : a css class or directly css attributes
const highlight_decoration = Decoration.mark({
// attributes: {style: "background-color: red"}
class: 'red_back'
});
// your editor's view
let main_view = new EditorView({
extensions: [highlight_extension]
});
// this is where the change takes effect by the dispatch. The of method instanciate the effect. You need to put this code where you want the change to take place
main_view.dispatch({
effects: highlight_effect.of([highlight_decoration.range(cursor.value.from, cursor.value.to)])
});
Hope it will help you to implement what you want ;)
Have a look at #codemirror/search.
Specifically, the source code implementation of Selection Matching may be of use for you to adapt.
It uses Decoration.mark over a range of text.
You can use SearchCursor to iterate over ranges that match your pattern (or RegExpCursor)
Use getSearchCursor, something like this:
var cursor = cmEditor.getSearchCursor(keyword , CodeMirror.Pos(cmEditor.firstLine(), 0), {caseFold: true, multiline: true});
if(cursor.find(false)){ //move to that position.
cmEditor.setSelection(cursor.from(), cursor.to());
cmEditor.scrollIntoView({from: cursor.from(), to: cursor.to()}, 20);
}
Programmatically search and select a keyword
Take a look at getSearchCursor source code it it give some glow about how it works and its usage.
So use getSearchCursor for finding text and optionally use markText for highlighting text because you can mark text with setSelection method of editor.
Selection Marking Demo
var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
lineNumbers: true,
styleSelectedText: true
});
editor.markText({line: 6, ch: 26}, {line: 6, ch: 42}, {className: "styled-background"});
And it seem this is what you are looking for:
codemirror: search and highlight multipule words without dialog
RegExpCursor is another option that you can use:
new RegExpCursor(
text: Text,
query: string,
options⁠?: {ignoreCase⁠?: boolean},
from⁠?: number = 0,
to⁠?: number = text.length
)
Sample usage at:
Replacing text between dollar signs for Mathml expression.

Word wrapping inside a TitledPane that is inside VBox

I have TitledPanes which contain large amounts of text. The TitledPanes are put inside of a VBox to lay them out vertically. But, when placed in a VBox, the TitlePane's width becomes the full width of the text instead of wrapping the text. How do I make it so that TitlePane's width is that of the available area, wrapping it's content, if necessary?
In this example, the text wrapping works as intended, but there's no VBox, so you can't have more than one TitledPane.
package nimrandsLibrary.fantasyCraft.characterBuilder
import javafx.scene.control._
import javafx.scene.layout._
import javafx.scene._
class TiltedTextExample extends javafx.application.Application {
def start(stage : javafx.stage.Stage) {
val titledPane = new TitledPane()
titledPane.setText("Expand me!")
val label = new Label("Some really long text here. Some really long text here. Some really long text here. Some really long text here. Some really long text here. Some really long text here. Some really long text here. Some really long text here. Some really long text here. Some really long text here. Some really long text here. Some really long text here. Some really long text here. Some really long text here. Some really long text here.")
label.setWrapText(true)
titledPane.setContent(label)
stage.setScene(new Scene(titledPane, 300, 300))
stage.show()
}
}
object TiltedTextExample extends App {
javafx.application.Application.launch(classOf[TiltedTextExample])
}
In this example, the TitledPane is placed inside a VBox so that multiple TitlePanes can be added and stacked vertically. Inexplicably, this breaks the word wrapping behavior.
package nimrandsLibrary.fantasyCraft.characterBuilder
import javafx.scene.control._
import javafx.scene.layout._
import javafx.scene._
class TiltedTextExample extends javafx.application.Application {
def start(stage : javafx.stage.Stage) {
val titledPane = new TitledPane()
titledPane.setText("Expand me!")
val label = new Label("Some really long text here. Some really long text here. Some really long text here. Some really long text here. Some really long text here. Some really long text here. Some really long text here. Some really long text here. Some really long text here. Some really long text here. Some really long text here. Some really long text here. Some really long text here. Some really long text here. Some really long text here.")
label.setWrapText(true)
titledPane.setContent(label)
val vBox = new VBox()
vBox.getChildren().add(titledPane)
stage.setScene(new Scene(vBox, 300, 300))
stage.show()
}
}
object TiltedTextExample extends App {
javafx.application.Application.launch(classOf[TiltedTextExample])
}
I have to delve deep (looking through the JavaFX code itself), but I found a workable, but not ideal, solution.
The problem basically boiled down to the fact that the TitledPane was not coded to support a horizontal content bias (where its min/preferred height is calculated based on its available width). So, even though its content, the Label, supports this functionality, its rendered mote by the TitledPane, which calculates its minimum and preferred dimensions without the consideration of the other, in which case the Label control simply requests a width and height necessary to display all its text on one line.
To overrride this behavior, I had to override three functions, two in the TitledPane itself, and one in the TitlePane's default skin (which actually does the dimension calculations for TitledPane). First, I override the getContentBias return HORIZONTAL. Then, I override the getMinWidth to return 0. Finally, I update the skin's computePrefHeight function to take into account the available width when its asking its children how much height they need.
Here is the code:
package nimrandsLibrary.fantasyCraft.characterBuilder
import javafx.scene.control._
import javafx.scene.layout._
import javafx.scene._
class TiltedTextExample extends javafx.application.Application {
private def createTitledPane(title: String) = {
val titledPane = new HorizontalContentBiasTitledPane()
titledPane.setText(title)
val label = new Label("Some really long text here. Some really long text here. Some really long text here. Some really long text here. Some really long text here. Some really long text here. Some really long text here. Some really long text here. Some really long text here. Some really long text here. Some really long text here. Some really long text here. Some really long text here. Some really long text here. Some really long text here.")
label.setWrapText(true)
titledPane.setContent(label)
titledPane.setExpanded(false)
titledPane
}
def start(stage: javafx.stage.Stage) {
val vBox = new VBox()
vBox.getChildren().add(createTitledPane("Expand Me #1"))
vBox.getChildren().add(createTitledPane("Expand Me #2"))
vBox.getChildren().add(createTitledPane("Expand Me #3"))
stage.setScene(new Scene(vBox, 300, 300))
stage.show()
}
}
object TiltedTextExample extends App {
javafx.application.Application.launch(classOf[TiltedTextExample])
}
class HorizontalContentBiasTitledPane extends javafx.scene.control.TitledPane {
override def getContentBias() = javafx.geometry.Orientation.HORIZONTAL
override def computeMinWidth(height: Double) = 0
this.setSkin(new com.sun.javafx.scene.control.skin.TitledPaneSkin(this) {
private val getTransitionMethod = classOf[com.sun.javafx.scene.control.skin.TitledPaneSkin].getDeclaredMethod("getTransition")
getTransitionMethod.setAccessible(true)
override protected def computePrefHeight(width: Double) = {
val contentContainer = getChildren().get(0)
val titleRegion = getChildren().get(1)
val headerHeight = Math.max(com.sun.javafx.scene.control.skin.TitledPaneSkin.MIN_HEADER_HEIGHT, snapSize(titleRegion.prefHeight(-1)));
var contentHeight = 0.0;
if (getSkinnable().getParent() != null && getSkinnable().getParent().isInstanceOf[com.sun.javafx.scene.control.skin.AccordionSkin]) {
contentHeight = contentContainer.prefHeight(width);
} else {
contentHeight = contentContainer.prefHeight(width) * getTransitionMethod.invoke(this).asInstanceOf[java.lang.Double].toDouble
}
headerHeight + snapSize(contentHeight) + snapSpace(getInsets().getTop()) + snapSpace(getInsets().getBottom());
}
})
}
This works for my use case, but there are some limitations, some of which can be worked around, and some that cannot.
This code is brittle, as it depends on specific implementation details of Java FX, especially the part where I have to call a private method on TitlePane's default skin. I don't see any easy way to fix this, other than to re-implement TitlePane's default skin in its enteritety (which I guess isn't too difficult, since the code is available).
Clamping the minWidth of the TitlePane to 0 it potentially problematic. A more robust algorithm might be necessary, depending on the use case.
Similarly, hard-coding title pane's content bias to HORIZONTAL isn't ideal. A more robust solution would be to use the content bias of its content, and make the dimension calculations of the control work based on that content bias.
Despite the limitations, this code seems to work for me, and I think the modifications to make it more robust are fairly straightforward. However, if anyone has a less drastic solution, or can improve on mine, please contribute.
Use Text instead of label. Example is as below,
#Override
public void start(Stage primaryStage) {
StackPane root = new StackPane();
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.setTitle("Hello World!");
VBox vBox = new VBox();
TitledPane tPane = new TitledPane();
tPane.setText("expand me");
tPane.setPrefWidth(root.getWidth());
Text text = new Text();
text.setText("Some really long text here. Some really long text here. Some really long text here. Some really long text here. Some really long text here. Some really long text here. Some really long text here. Some really long text here. Some really long text here. Some really long text here. Some really long text here. Some really long text here. Some really long text here. Some really long text here. Some really long text here.");
text.setWrappingWidth(tPane.getPrefWidth());
tPane.setContent(text);
vBox.getChildren().add(tPane);
root.getChildren().add(vBox);
primaryStage.show();
}
The titlepane of javafx will take the maximum width of its children.
Result:

pygtk menubar with fixed

I'm new to pyGTK, and now I'm trying to create a menubar with a fixed layout, but I only get a background on the items, not on the entire bar. My code:
import gtk
class App(gtk.Window):
def __init__(self):
super(App,self).__init__()
self.set_size_request(640,480)
self.set_position(gtk.WIN_POS_CENTER)
menubar = gtk.MenuBar()
menu_file= gtk.Menu()
menuitem_file = gtk.MenuItem("File")
menuitem_file.set_submenu(menu_file)
menuitem_exit = gtk.MenuItem("Exit")
menuitem_exit.connect("activate",gtk.main_quit)
menu_file.append(menuitem_exit)
menubar.append(menuitem_file)
fixed = gtk.Fixed()
vbox = gtk.VBox(False, 2)
vbox.pack_start(menubar, False, False, 0)
fixed.add(vbox)
self.add(fixed)
self.connect("destroy",gtk.main_quit)
self.show_all()
App ()
gtk.main ()
You need to make vbox request size, e.g. add vbox.set_size_request (300,50) and see the difference. It is not correct size, but then I don't know why you use gtk.Fixed at all. In 99.95% of case you don't need gtk.Fixed. And especially if you are new to GTK+ you might think you need it while you actually don't.

GWT: TabLayoutPanel with custom tabs does not display correctly

I have a TabLayoutPanel where I am putting custom widgets in for the tabs to be able to display some images next to the text. I originally worked with TabPanel and using custom HTML for the tab text, but custom tab widgets allows me to modify the image on the fly as needed.
My tab widget is essentially a HorizontalPanel, a number of small images, and a line of text. The problem I'm having is that the tab doesn't want to stick to the bottom of the tab bar like normal. The tab is getting positioned at the top of the space reserved for the tab bar, and there's a gap between it and the bottom of the tab bar. I uploaded an image of the problem to http://imgur.com/fkSHd.jpg.
Is there some style that I need to apply to custom widget tabs to make them appear correctly?
In my brief experience, the newer standards mode panels (they all end in "LayoutPanel") don't get along with the older ones (the ones that just end in "Panel"). So you might consider trying a DockLayoutPanel instead of the HorizontalPanel, and it may be more cooperative.
See https://developers.google.com/web-toolkit/doc/latest/DevGuideUiPanels, particularly the section called "What won't work in Standards Mode?":
HorizontalPanel is a bit trickier. In some cases, you can simply
replace it with a DockLayoutPanel, but that requires that you specify
its childrens' widths explicitly. The most common alternative is to
use FlowPanel, and to use the float: left; CSS property on its
children. And of course, you can continue to use HorizontalPanel
itself, as long as you take the caveats above into account.
After a bit more research, I found the answer here: https://groups.google.com/d/msg/google-web-toolkit/mq7BuDaTNgk/wLqPm5MQeicJ. I had to use InlineLabel or InlineHTML widgets instead of normal Label or HTML widgets. I've tested this solution and it does exactly what I want. I pasted the code of the class below for completeness. Note two things here:
The "float" attribute cannot be set on the last element (the InlineLabel) or the incorrect drawing condition occurs again.
The code could be cleaned up a bit further by having the class extend directly from FlowPanel instead of making it a composite containing a FlowPanel.
package com.whatever;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Float;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.InlineLabel;
public class StatusTab extends Composite
{
public interface StatusImages extends ClientBundle
{
public static StatusImages instance = GWT.create(StatusImages.class);
#Source("images/status-green.png")
ImageResource green();
#Source("images/status-red.png")
ImageResource red();
}
private final ImageResource greenImage;
private final ImageResource redImage;
private final FlowPanel flowPanel;
public LinkStatusTab(String text, int numStatuses) {
greenImage = StatusImages.instance.green();
redImage = StatusImages.instance.red();
flowPanel = new FlowPanel();
initWidget(flowPanel);
for (int i = 0; i < numStatuses; i++)
{
Image statusImg = new Image(redImage);
statusImg.getElement().getStyle().setMarginRight(3, Unit.PX);
statusImg.getElement().getStyle().setFloat(Float.LEFT);
flowPanel.add(statusImg);
}
flowPanel.add(new InlineLabel(text));
}
/**
* Sets the image displayed for a specific status entry.
*/
public void setStatus(int which, boolean status)
{
Image image = (Image)flowPanel.getWidget(which);
if (status)
image.setResource(greenImage);
else
image.setResource(redImage);
}
}

While switching to windows classic theme combo contribution item shirnks

I do have a problem with IToolbarManager. I have added a combo & spinner ot toolbar of a view like this
IToolbarManager mgr = getViewSite().getActionBars().getToolBarManager();
mgr.add(spinnerCntrAction);
spinnerCntrAction = new ControContribution(){
public Control createControl(){
//Creates composite
//Create a spinner and add that to composite
//return composite
}
};
In windows XP/Vista themes this spinner is shown correctly. But when program is run under windows classic theme , the spinner is shrinked and not shown correctly.
Is this a known problem ? Do you know any workaround/patch for this ?
Thanks
Jijoy
This is a bug in SWT. See http://dev.eclipse.org/newslists/news.eclipse.platform.swt/msg44671.html
Here is a workaround:
mgr.add(new DummyAction());
private static class DummyAction extends Action {
DummyAction() {
setEnabled(false);
setText(" ");
}
}
...
mgr.add(spinnerCntrAction);
This will cause the toolbar manager to make all control contributions the same size as the Action, so adjust the number of spaces in the Action text to get the desired result.