Assign widget to class variable to improve performance? - performance

I have a custom CircularProgressIndicator widget wrapped inside the Padding widget. I want to use this custom widget say 100 times inside build method of another (main) widget.
To avoid repetition in code I have assigned it to top level class variable, and then referenced it in wherever I needed.
Apart from code readability and preventing repetition,
will this also improve overall performance of my "main" widget?

TLDR Extract your widget to a class (see here why) and try to create a const constructor for it.
Why those two advices:
Widgets created from classes can be optimized by the flutter framework among other benefits.
const Objects are created at compile time and for the same configuration the same object is returned.
const CustomCircularProgressIndicator()
Say you have your widget constructor like above, anywhere you use this CustomCircularProgressIndicator the same instance of the widget will be used.
Those aren't all the optimizations that can be done but they are a good start. For more optimizations like scoping your UI rebuilds see this great answer

Related

IDWriteInlineObject::GetOverhangMetrics method is never called

I have implemented IDWriteInlineObject interface to draw ID2D1Bitmap as inline object inside a text. Everything works fine except one: my implementation's GetOverhangMetrics method never get called.
I've also tried a sample from DirectWrite SDK samples (PadWrite Layout Sample, https://msdn.microsoft.com/en-us/library/windows/desktop/dd941792%28v=vs.85%29.aspx). And got the same result ('InlineImage::GetOverhangMetrics' in InlineImage.cpp is never called).
Therefore my question is why one's implementation of GetOverhangMetrics method never get called?
I admit I haven't tested this, so just a thought - I think it's possible it's called when you ask for overhang metrics of layout itself, so when layout method GetOverhangMetrics() is called it feels like it would accumulate not only regular text overhang boxes, but inline object boxes too.
GetOverhangMetrics is called for an inline object when the same method is called explicitly on the text layout object containing this inline object.

Fast way to obtain underlying java component for controls in matlab

I'd like to obtain a reference to underlying java components for the controls I have in my GUI so as to customize their appearance.
I know about findjobj from Yair Altman which works really well:
myLink = uicontrol('String', '<html><u>Button that looks like a link.</u></html>');
jObj = findjobj(myLink);
jObj.setContentAreaFilled(0);
Unfortunately this solution is quite slow when there are a "lot" of controls to customize (because it has to parse the full hierarchy of objects in the figure and this for each control to customize).
Moreover the figure must be visible (else controls are not instantiated and java references cannot be found). Plus it must be moved of screen to avoid users to touch it while findjobj is running (sometimes make things crash because findjobj somehow relies on position of controls to find them while internally calling also drawnow which updates positions) ...
On some machines, even with only a few controls to customize, it can be up to 10 seconds before to have the figure to appear (most of the time is spent in findjobj).
I also know about uicomponent again from Yair Altman to directly create controls and get the handle to the underlying java component in one shot:
[myLink, jObj] = uicomponent('Style', 'JButton', 'String', '<html><u>Button that looks like a link.</u></html>');
jObj.setContentAreaFilled(0);
Unfortunately here the parent property can only be a figure and of course my controls are placed in gui layout containers to handle resizing and many other things nicely (and gui layout containter are not valid hg-handles for uicomponent to work)...
So was wondering if there could be any other fast solution to get underlying java components for controls in my GUI ? ... NB: I mainly only need to have buttons that looks like hyperlinks or animated gif (i.e. borderless buttons with htlm text/img inside).
This isn't a direct answer to your question, but I've built several GUIs that are based around GUI Layout Toolbox and that contain Java swing components. I typically arrange things so that the GUI Layout container (HBox, VBox, Grid etc) has a uipanel as a child, and then the uipanel has the Java swing component as a child.
You can typically parent a Java component to the uipanel in exactly the same way as parenting it to a figure (unlike a GUI Layout container), and it's no problem to parent a uipanel to a GUI Layout container.
So, for example, to add a button with a dropdown menu (no menu items, so it won't do anything, but just to illustrate):
>> u = uipanel;
>> ddbuttonclass = 'com.mathworks.widgets.DropdownButton';
>> ddbutton = javaObjectEDT(ddbuttonclass);
>> [jddbutton, hjddbutton] = javacomponent(ddbutton, [30,30, 60, 30], u);
Now you can parent u to a GUI Layout container, and you get all the nice resizing.
I'm not that familiar with Yair's uicomponent, but if you can get the handle of the java component somehow, you should be able to use something like the above.
PS If you want his direct input, #Yair is sometimes active on SO - he may get notified if I mention his name. If you're doing a lot of Java/MATLAB GUI work, I'd also recommend buying his book.
UICOMPONENT was designed to be a direct replacement of both Matlab's built-in UICONTROL and JAVACOMPONENT functions. This means that you can directly place a UICOMPONENT within panels, even those created by the GUI Layout toolbox.
You might need to cast the layout panel's handle to double (double(hPanel)) on some Matlab releases but that's about it:
[myLink, jObj] = uicomponent('Parent',hPanel, ...);
[myLink, jObj] = uicomponent('Parent',double(hPanel), ...); % on some Matlab releases
You could also use JAVACOMPONENT directly, but it doesn't really give you any benefits over UICOMPONENT, since UICOMPONENT uses JAVACOMPONENT under the hood and also adds important functionality (such as ensuring the component is placed on the EDT, and merging important properties from the Matlab wrapper).
As for FINDJOBJ, you can speed it up a bit by specifying the target object class using the 'class' parameter. But if your figure contains hundreds of controls it might still be slow. To this day, close to 10 years after my first version of FINDJOBJ, I still do not know of a direct way to get the underlying Java object. I assume there is one that is used internally by MathWorks, but I do not know it.
As #SamRoberts mentioned, this is all discussed in my book...

Simplest way to identify an Interface Builder element in code

I am building a UI in Interface Builder and am looking for the simplest (least code) way to identify an element from code.
I'd like to avoid using outlets because frankly I detest visual programming and don't want to pollute my class space with countless outlet properties. Is there some unique string identifier I can assign to static elements that I can either reference directly or easily look up from code?
Ideally I just want to look up an object by its id like I can do in JavaScript:
document.getElementById('myIdentifier');
I agree with rightfold that outlets are the best solution, but there is an answer that addresses your question directly: you can use the (integer) tag property of UIView (setting it either in IB or in code), and then you can fetch the view with the method UIView -viewWithTag:.
Successive calls to -viewWithTag: will iterate through the subviews that have the given tag. Because it's an integer, you'll probably want to use named constants for tag references in code, but unfortunately there's no way (that I am aware of) to use constants in that manner in IB.
The default value for the tag property is 0, so avoid using that as a semantically meaningful tag.

Windows phone 7 -- programmatically create the initial UI?

I want to create the UI in code when the app starts up. What would be the natural place for this code?
You would probably want to put it in the page constructor right after the InitializeComponent() call. I don't see a need to do this since you have the XAML designer at hand anyway - it will eventually create a small performance overhead because you are building the page twice - once when it's loaded and the second time when you add additional components.
I am assuming your intention is to have a total control of the application resources in this manner. You get one entry point, where you can initialize a view, as well as the other operational classes, that make your application tick.
So my answer would be this:
In App() initialize an IoC right a way.
Then let default code, such as InitializeComponent() do it's job. Remove InitializePhoneApplication() and insert your view initialization logic there.

Talking Among GWT Panels using UIBinder Layout

New to GWT here...
I'm using the UIBinder approach to layout an app, somewhat in the style of the GWT Mail sample. The app starts with a DockLayoutPanel added to RootLayoutPanel within the onModuleLoad() method. The DockLayoutPanel has a static North and a static South, using a custom center widget defined like:
public class BigLayoutWidget extends ResizeComposite {
...
}
This custom widget is laid out using BigLayoutWidget.ui.xml, which in turn consists of a TabLayoutPanel (3 tabs), the first of which contains a SplitLayoutPanel divided into WEST (Shortcuts.ui.xml) and CENTER (Workpanel.ui.xml). Shortcuts, in turn, consists of a StackLayoutPanel with 3 stacks, each defined in its own ui.xml file.
I want click events within one of Shortcuts' individual stacks to change the contents of Workpanel, but so far I've only been able to manipulate widgets within the same class. Using the simplest case, I can't get a button click w/in Shortcuts to clear the contents of Workpanel or make WorkPanel non-visible.
A few questions...
Is ResizeComposite the right type of class to extend for this? I'm following the approach from the Mail example for TopPanel, MailList, etc, so maybe not?
How can I make these clicks manipulate the contents of panels in which they do NOT reside?
Are listeners no longer recommended for handling events? I thought I saw somewhere during compilation that ClickHandlers are used these days, and the click listener "subscription" approach is being deprecated (I'm mostly using #UiHandler annotations)
Is there an easy way to get a handle to specific elements in my app/page? (Applying the "ID" field in the UI.XML file generates a deprecation warning). I'm looking for something like a document.getElementById() that get me a handle to specific elements. If that exists, how do I set the handle/ID on the element, and how can I then call that element by name/id?
Note that I have the layout itself pretty well nailed; it's the interaction from one ui.xml modularized panel to the next that I can't quite get.
Thanks in advance.
If you don't have a use for resizing events than just use Composite
What you want is what the GWT devs called message bus (implemented as HandlerManager). You can get a nice explanation in the widely discussed (for example, on the GWT Google Group, just search for 'mvp') presentation by Ray Ryan from Google I/O 2009 which can be found here. Basically, you "broadcast" an event on that message bus and then a Widget listening for that event gets the message and does its stuff.
Yep, *Handlers are the current way of handling events - the usage is basically the same so migration shouldn't be a problem (docs). They changed it so that they could introduce custom fields in the future, without breaking existing code.
If you've set an id for any DOM element (for Widgets I use someWidget.getElement().setId(id), usually in combination with DOM.createUniqueId()) you can get it via GWT.get(String id). You'll get then a RootPanel which you'll have to cast to the right Widget class - as you can see it can get a little 'hackish' (what if you change the type of the Widget by that id? Exceptions, or worse), so I'd recommend sticking with MVP (see the first point) and communicating via the message bus. Remember however, that sometimes it's also good to aggregate - not everything has to be handled via the message bus :)
Bottom line is I'd recommend embracing MVP (and History) as soon as possible - it makes GWT development much easier and less messy :) (I know from experience, that with time the code starts to look like a nightmare, if you don't divide it into presentation, view, etc.)

Resources