How do you hide a Gtk# window? - mono

How can I hide a Gtk# window that I created in MonoDevelop? I tried the following but it doesn't work:
public MainWindow (): base (Gtk.WindowType.Toplevel)
{
Build();
this.HideAll();
this.Visible = false;
}
Solution
Calling HideAll() outside the constructor, as tomlog suggested, works. If you want to do some work before showing the window you can
Add an event handler to Window.Shown before calling Build()
Do the work before calling Build() (probably the better alternative in most cases)

I don't think you can call HideAll in the constructor, because the form is not fully initialized and therefore not visible yet.

Related

Exception when I change to other scene in KorGE

com.soywiz.korinject.AsyncInjector$NotMappedException: Class 'class ChooseCampaign (Kotlin reflection is not available)' doesn't have constructors RequestContext(initialClazz=class ChooseCampaign (Kotlin reflection is not available))
Above exception was threw when I compiled current code. And I dont know how fix it and what does it means.
My code:
textButton {
position(0, 128)
text = "Play"
onClick {
println("Play")
launchImmediately {
sceneContainer.changeTo<ChooseCampaign>()
}
}
}
How it fix?
When using Scenes in KorGE, you are using the korinject dependency injector indirectly.
And that injector requires manual mapping. If you are using Modules, you can configure those mappings at the Module.init method.
Check this sample: https://github.com/korlibs/korge-samples/blob/1771b7ca7f4440e1a368ff4b441e97bf62e08b8d/sample-scenes/src/commonMain/kotlin/main.kt#L15-L23
In your case, once you get the Injector instance, you can map a scene like this:
mapPrototype { ChooseCampaign(get()) }
You have to put as many get() as parameters your ChooseCampaign constructor has.
In the case you are not using modules, the place to put the mapping is different, and you need to get the Injector instance.
In your suspend fun main() = Korge { block, you have the Stage singleton injected. This is the root view that has a reference to the Views singleton.
So there you can just access the injector like this: this.views.injector
You can then map your scenes whenever you like, though I suggest you do to it at the beginning of the application.

Why are variables declared after root automatically added to UI in TornadoFx?

I have stumbled upon a behaviour in TornadoFx that doesn't seem to be mentioned anywhere (I have searched a lot) and that I'm wondering about.
If I define a view like this with the TornadoFx builders for the labels:
class ExampleView: View() {
override
val root = vbox{ label("first label") }
val secondLabel = label("second label")
}
The result is:
That is, the mere definition of secondLabel automatically adds it to the rootof the scene.
However, if I place this definition BEFORE the definition of root...
class ExampleView: View() {
val secondLabel = Label("second label")
override
val root = vbox{ label("first label") }
}
... or if I use the JavaFx Labelclass instead of the TornadoFx builder ...
class ExampleView: View() {
override
val root = vbox{ label("first label") }
val secondLabel = Label("second label")
}
... then it works as I expect:
Of course, I can simply define all variables in the view before I define the rootelement but I'm still curious why this behaviour exists; perhaps I am missing some general design rule or setting.
The builders in TornadoFX automatically attach themselves to the current parent in the scope they are called in. Therefore, if you call a builder function on the View itself, the generated ui component is automatically added to the root of that View. That's what you're seeing.
If you really have a valid use case for creating a ui component outside of the hierarchy it should be housed in, you shouldn't call a builder function, but instead instantiate the element with it's constructor, like you did with Label(). However, the use cases for such behavior are slim to none.
Best practice is to store value properties in the view or a view model and bind the property to the ui element using the builders. You then manipulate the value property when needed, and the change will automatically update in the ui. Therefore, you very very seldom have a need to access a specific ui element at a later stage. Example:
val myProperty = SimpleStringProperty("Hello world")
override val root = hbox {
label(myProperty)
}
When you want to change the label value, you just update the property. (The property should be in an injected view model in a real world application).
If you really need to have a reference to the ui element, you should declare the ui property first, then assign to it when you actually build the ui element. Define the ui property using the singleAssign() delegate to make sure you only assign to it once.
var myLabel: Label by singleAssign()
override val root = hbox {
label("My label) {
myLabel = this
}
}
I want to stress again that this is very rarely needed, and if you feel you need it you should look to restructure your ui code to be more data driven.
Another technique to avoid storing references to ui elements is to leverage the EventBus to listen for events. There are plenty of examples of this out there.

btn.setOnClickListener() how does it calls

I am beginner in programing, and I know how btn.setOnClickListener{} function works (curly brackets).
But there is other tipe of function btn.setOnClickListener() - brackets are not curly. I do not know how and when I should use this tipe of functions. How does such type of function calls? I would like to learn more about it but I do not know how to google it
Answer: "If a function has only one parameter, and this is a function, the parentheses can be deleted"
According to : https://antonioleiva.com/lambdas-kotlin-android/
If a function has only one parameter, and this is a function, the parentheses can be deleted
Instead of having empty parentheses, we can better delete them:
view.setOnClickListener { v -> toast("Hello") }
If the function’s last parameter is a function, it can go outside the parentheses
Therefore, we can extract the listener as follows:
view.setOnClickListener() { v -> toast("Hello") }
If we had more parameters, the rest of the parameters would go inside the parentheses, even if these were functions. Only the last parameter can be extracted.
Both types are equivalent:
button.setOnClickListener {
// ......................
}
button.setOnClickListener(View.OnClickListener {
// ......................
})
but the 1st one is the preferred way to go.
Even if you write the 2nd one, if you hover the mouse over View.OnClickListener, Android Studio will pop up this message:
Redundant SAM constructor
and if you press Alt-Enter you will be prompted:
Remove redundant SAM constructor
and if you click on it then View.OnClickListener will be removed.
Again by pressing Alt-Enter you will be prompted:
Move lambda argument out of parentheses
and if you click on it then you will get the 1st type.
So don't worry about it, use the 1st type and you will be fine.
Answer: "If a function has only one parameter, and this is a function, the parentheses can be deleted"
setOnClickListener(Interface i)
This is Method of View Class in Android.
1. setOnClickListener is method of that class which except only interface as a parameter.
2. or else you have to implement that interface in your class like given example.
Go through Anonymous Class implement process.
the thing is that. either you pass interface object or you have to implement onClick method interface OnClickListener.
1. When you want to implement in class use this
btnView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
2. Pass interface object of onClickListener
appView.setOnClickListener(); ;----> pass interface object

How do I implement my own animation library in Aurelia?

Let's say I want to use something like JQuery UI to write some animations when an element is added or removed from the DOM. I see in this article that I could use the animator interface to write my own animations using the enter and leave functions. This same article uses the example of view-slot.js to show how this interface is utilized. I can even see this dependency being injected here, like this
import {Animator} from './animator';
...
constructor(anchor: Node, anchorIsContainer: boolean, animator?: Animator = Animator.instance) {
How can I supply my own implementation of the Animator interface here? I used the Greensock aurelia plugin, which is another javascript animation library, as an example of how I might do this only to find that it's using its own version of animator.js with Greensock animations written into it. I was expecting to find something that would supply a custom implementation of the animator interface to the view-slot constructor but came up empty.
Any help is appreciated.
Found it. You need to run configureAnimator on the templating engine which resides inside Aurelia's DI container. Below example is taken from animator-css which is an implementation of the animator interface. This piece of code runs during the startup process as a plugin.
export function configure(config: Object, callback?:(animator:CssAnimator) => void): void {
let animator = config.container.get(CssAnimator);
config.container.get(TemplatingEngine).configureAnimator(animator);
if (typeof callback === 'function') { callback(animator); }
}
https://github.com/aurelia/animator-css/blob/master/src/index.js

Add methods to an assigned closure with GroovyDSL

Geb uses a static field called content to define the contents of a page or module. The value of the content field is a closure.
class GebishOrgHomePage extends Page {
static content = {
manualsMenu {
module MenuModule, $("#header-content ul li", 0)
}
links { $('.link-list li a') }
}
}
Intellij already has support for this content dsl, however it does not support the module and moduleList methods. This causes limited auto-complete support when working with modules.
To fix this I'd like to write a GroovyDSL script that adds the missing method definitions to the content closure and its nested closures. However, I've no idea how to add methods to a closure that is not passed to a method, since enclosingCall requires a concrete method name.
And the other thing is that those methods must have a generic return type like this:
<T extends Module> T module(Class<T> m) {
// return an instance of T
}
If you use the latest snapshot then module() calls will be understood by your IDE. This is down to moving module() to Navigator exactly for what you are after - autocompletion and strong typing.
Have a look at the current version of section 6.4 of the Book of Geb. The moduleList() will be gone in a future release and that section explains what to use instead. The module() method taking a map argument to initialise module properties will also go, you now initialise the module yourself and pass the instance to module() and there is an example of doing this in 6.4. Thanks to all that you will get autocompletion around module defintions and usage in IntelliJ.