-
Notifications
You must be signed in to change notification settings - Fork 193
/
multi_window.rs
157 lines (147 loc) · 5.32 KB
/
multi_window.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/// TODO: Fix mutli window to work properly as web instance.
use orbtk::prelude::*;
// constants
pub static ID_WINDOW_1: &str = "Window 1";
pub static ID_WINDOW_1_BUTTON: &str = "Window-1 Button";
pub static ID_WINDOW_1_STACK: &str = "Window-1 Stack";
pub static ID_WINDOW_1_TEXTBLOCK: &str = "Window-1 TextBlock";
pub static ID_WINDOW_2: &str = "Window 2";
pub static ID_WINDOW_2_BUTTON: &str = "Window-2 Button";
pub static ID_WINDOW_2_STACK: &str = "Window-2 Stack";
pub static ID_WINDOW_2_TEXTBLOCK: &str = "Window-2 TextBlock";
pub static ID_WINDOW_3: &str = "Window 3";
pub static ID_WINDOW_3_BUTTON: &str = "Window-3 Button";
pub static ID_WINDOW_3_STACK: &str = "Window-3 Stack";
pub static ID_WINDOW_3_TEXTBLOCK: &str = "Window-3 TextBlock";
// [Window1View]
widget!(Window1View<Window1State>);
impl Template for Window1View {
fn template(self, id: Entity, ctx: &mut BuildContext) -> Self {
self.child(
Stack::new()
.id(ID_WINDOW_1_STACK)
.name(ID_WINDOW_1_STACK)
.child(
TextBlock::new()
.id(ID_WINDOW_1_TEXTBLOCK)
.name(ID_WINDOW_1_TEXTBLOCK)
.text("Window 1")
.margin(4.0)
.build(ctx),
)
.child(
Button::new()
.id(ID_WINDOW_1_BUTTON)
.name(ID_WINDOW_1_BUTTON)
.on_click(move |states, _| {
states.get_mut::<Window1State>(id).toggle_window();
true
})
.margin(4.0)
.text("Show window")
.build(ctx),
)
.build(ctx),
)
}
}
// [Window1ViewState]
#[derive(AsAny, Default)]
struct Window1State {
/// Toggle visibility of given window.
toggle_window: bool,
}
impl Window1State {
fn toggle_window(&mut self) {
self.toggle_window = true;
}
}
impl State for Window1State {
fn update(&mut self, _: &mut Registry, ctx: &mut Context) {
if self.toggle_window {
Button::enabled_set(&mut ctx.child(ID_WINDOW_1_BUTTON), false);
ctx.show_window(|ctx| {
Window::new()
.id(ID_WINDOW_3)
.name(ID_WINDOW_3)
.title("OrbTk - Dialog")
.position((120.0, 120.0))
.size(420.0, 75.0)
.child(
Stack::new()
.id(ID_WINDOW_3_STACK)
.name(ID_WINDOW_3_STACK)
.child(
TextBlock::new()
.id(ID_WINDOW_3_TEXTBLOCK)
.name(ID_WINDOW_3_TEXTBLOCK)
.text("New window")
.margin(4.0)
.build(ctx),
)
.build(ctx),
)
.build(ctx)
});
self.toggle_window = false;
}
}
}
/// The multi-windows example.
///
/// This OrbTk example application implements two side by side
/// windows:
///
/// * [Window 1]: The view will show an ordenary window that has an
/// associated state. Via a button inside its view you may trigger an
/// associated function, that will open a new child window (`dialog`).
///
/// * [Window 2]: The view will show another ordenary window. This one
/// is static, no state is associated.
pub fn main() {
// use this only if you want to run it as web application.
// orbtk::initialize();
Application::new()
.window(|ctx| {
Window::new()
.id(ID_WINDOW_1)
.name(ID_WINDOW_1)
.title("OrbTk - multi-window 1")
.position((100.0, 100.0))
.size(420.0, 130.0)
.child(Window1View::new().build(ctx))
.build(ctx)
})
.window(|ctx| {
Window::new()
.id(ID_WINDOW_2)
.name(ID_WINDOW_2)
.title("OrbTk - multi-window 2")
.position((600.0, 100.0))
.size(420.0, 130.0)
.child(
Stack::new()
.id(ID_WINDOW_2_STACK)
.name(ID_WINDOW_2_STACK)
.child(
TextBlock::new()
.id(ID_WINDOW_2_TEXTBLOCK)
.name(ID_WINDOW_2_TEXTBLOCK)
.text("Window 2")
.margin(4.0)
.build(ctx),
)
.child(
Button::new()
.id(ID_WINDOW_2_BUTTON)
.name(ID_WINDOW_2_BUTTON)
.margin(4.0)
.text("Click me")
.build(ctx),
)
.build(ctx),
)
.build(ctx)
})
.run();
}