Unsure of the correct way to make multi-window application #3315
Replies: 2 comments 2 replies
-
If you really want to use different menus on each toplevel and on each modal window then the It also doesn't make sense to me the fact you are creating the menu on the public class MainTopLevel : Toplevel
{
public static int windowCount = 0;
public static bool quitApp;
public MainTopLevel()
{
var customMenuBar = new MenuBar (new MenuBarItem [] {
new MenuBarItem ("_File", new MenuItem [] {
new MenuItem($"Window {windowCount}", "", () => { }),
new MenuItem ("_Close Window", "", () => {
windowCount--;
Application.RequestStop();
}),
new MenuItem ("_Quit", "", () => {
// Now does what I wanted
quitApp = true;
Application.RequestStop ();
})
}),
});
Add(customMenuBar);
var window = new MainWindow($"Window - {windowCount}");
Add(window);
++windowCount;
}
}
public class MainWindow : Window
{
public MainWindow(string title) : base(title)
{
// Create login button
var pushWindowButton = new Button () {
Text = "P_ush Window",
Y = Pos.Center() - 1,
X = Pos.Center (),
};
pushWindowButton.Clicked += () =>
{
Application.Run<MainTopLevel>();
if (MainTopLevel.quitApp) {
Application.RequestStop();
}
};
var popWindowButton = new Button () {
Text = "P_op Window",
Y = Pos.Center() + 1,
X = Pos.Center (),
};
popWindowButton.Clicked += RequestStop;
Add(pushWindowButton, popWindowButton);
}
} |
Beta Was this translation helpful? Give feedback.
-
If I have understood you correctly you want a menu bar per window and the ability to pop modals over one another. This is well supported, here is how.
using Terminal.Gui;
using YourNamespace;
Application.Init();
Application.Run(new Window1());
Application.Shutdown(); Program.cs public Window1() {
InitializeComponent();
showWindow2MenuItem.Action = () =>
{
Application.Run(new Window2() {
X = Pos.Percent(25),
Y = Pos.Percent(25),
Width = Dim.Percent(50),
Height = Dim.Percent(50),
});
};
} Window1.cs public Window2() {
InitializeComponent();
showWindow3MenuItem.Action = () =>
{
MessageBox.Query("This window 3", "Window 3", "Ok");
};
closeWindow2MenuItem.Action = () =>
{
Application.RequestStop();
};
} Window2.cs When run it will look as follows: |
Beta Was this translation helpful? Give feedback.
-
Hey,
I am wanting to make an application where a single window is visible at a time and each window can have its own menu. Navigating between windows would be a push/pop style navigation similar to Xamarin.Forms (and others) PushModal, PopModal. So the idea is you start at a main window, push a second window, from that window push a third. Then you choose to go back so you pop and you go back to the second window as it was.
I was a little confused by
Window
having aMenuBar
but setting it doesn't make it appear as you need to add it. I can see in the code that adding aMenuBar
viaAdd(
will assign it to theMenuBar
property, but doing so causes the menu bar to be below the window title. Setting theWindow
Y=1` just pushes both down and leaves a black line up the top.Playing around with some things I got something to work, but unsure if this is making a mess of things and isn't how it's meant to work.
The general idea is I have a window
MainWindow
and it defines its own menu. Then a seperate TopLevel controls adding that menu to the visible stack itself. In final app I'd haveSecondWindow
,ThirdWindow
but for this example is justMainWindow
, and I seperate what window is what with a static in counter.The main top level looks like this (again, in future I'd abstract it out, or pass window to it)
and then to start the application I just call this,
Is the way I am pushing/poping ideal? Is there a better way? Is what I am doing here the reason why I can't access the menu via the F key?
Beta Was this translation helpful? Give feedback.
All reactions