1
1
use std:: path:: PathBuf ;
2
2
3
3
use super :: image_button:: ImageButton ;
4
+ use crate :: data:: WindowData ;
4
5
use crate :: { consts:: BUTTON_HIGHLIGHT_COMMAND , data:: ApplicationState , tools:: DrawingTools } ;
5
6
use druid:: {
6
7
widget:: { CrossAxisAlignment , Flex , MainAxisAlignment } ,
@@ -34,8 +35,12 @@ impl ToolBarWidget {
34
35
DrawingTools :: Select . to_string ( ) ,
35
36
)
36
37
. on_click ( |ctx, data : & mut ApplicationState , _env| {
38
+ let win_data = data
39
+ . windows
40
+ . get_mut ( & ctx. window_id ( ) )
41
+ . expect ( "Invalid WindowID" ) ;
37
42
let tool = DrawingTools :: Select ;
38
- data . mode = tool;
43
+ win_data . mode = tool;
39
44
ctx. submit_notification ( BUTTON_HIGHLIGHT_COMMAND . with ( tool. to_string ( ) ) ) ;
40
45
ctx. set_handled ( ) ;
41
46
} ) ,
@@ -48,8 +53,12 @@ impl ToolBarWidget {
48
53
DrawingTools :: Line . to_string ( ) ,
49
54
)
50
55
. on_click ( |ctx, data : & mut ApplicationState , _env| {
56
+ let win_data = data
57
+ . windows
58
+ . get_mut ( & ctx. window_id ( ) )
59
+ . expect ( "Invalid WindowID" ) ;
51
60
let tool = DrawingTools :: Line ;
52
- data . mode = tool;
61
+ win_data . mode = tool;
53
62
ctx. submit_notification ( BUTTON_HIGHLIGHT_COMMAND . with ( tool. to_string ( ) ) ) ;
54
63
ctx. set_handled ( ) ;
55
64
} ) ,
@@ -62,8 +71,12 @@ impl ToolBarWidget {
62
71
DrawingTools :: Rect . to_string ( ) ,
63
72
)
64
73
. on_click ( |ctx, data : & mut ApplicationState , _env| {
74
+ let win_data = data
75
+ . windows
76
+ . get_mut ( & ctx. window_id ( ) )
77
+ . expect ( "Invalid WindowID" ) ;
65
78
let tool = DrawingTools :: Rect ;
66
- data . mode = tool;
79
+ win_data . mode = tool;
67
80
ctx. submit_notification ( BUTTON_HIGHLIGHT_COMMAND . with ( tool. to_string ( ) ) ) ;
68
81
ctx. set_handled ( ) ;
69
82
} ) ,
@@ -76,8 +89,12 @@ impl ToolBarWidget {
76
89
DrawingTools :: Text . to_string ( ) ,
77
90
)
78
91
. on_click ( |ctx, data : & mut ApplicationState , _env| {
92
+ let win_data = data
93
+ . windows
94
+ . get_mut ( & ctx. window_id ( ) )
95
+ . expect ( "Invalid WindowID" ) ;
79
96
let tool = DrawingTools :: Text ;
80
- data . mode = tool;
97
+ win_data . mode = tool;
81
98
ctx. submit_notification ( BUTTON_HIGHLIGHT_COMMAND . with ( tool. to_string ( ) ) ) ;
82
99
ctx. set_handled ( ) ;
83
100
} ) ,
@@ -90,8 +107,12 @@ impl ToolBarWidget {
90
107
DrawingTools :: Eraser . to_string ( ) ,
91
108
)
92
109
. on_click ( |ctx, data : & mut ApplicationState , _env| {
110
+ let win_data = data
111
+ . windows
112
+ . get_mut ( & ctx. window_id ( ) )
113
+ . expect ( "Invalid WindowID" ) ;
93
114
let tool = DrawingTools :: Eraser ;
94
- data . mode = tool;
115
+ win_data . mode = tool;
95
116
ctx. submit_notification ( BUTTON_HIGHLIGHT_COMMAND . with ( tool. to_string ( ) ) ) ;
96
117
ctx. set_handled ( ) ;
97
118
} ) ,
@@ -114,7 +135,11 @@ impl ToolBarWidget {
114
135
. with_child (
115
136
ImageButton :: new ( save_icon, Size :: new ( 26.0 , 26.0 ) , String :: new ( ) ) . on_click (
116
137
move |ctx, data : & mut ApplicationState , _env| {
117
- save_to_file ( data, ctx) ;
138
+ let win_data = data
139
+ . windows
140
+ . get_mut ( & ctx. window_id ( ) )
141
+ . expect ( "Invalid WindowID" ) ;
142
+ save_to_file ( win_data, ctx) ;
118
143
ctx. set_handled ( ) ;
119
144
} ,
120
145
) ,
@@ -142,7 +167,7 @@ fn open_from_file(ctx: &mut druid::EventCtx) {
142
167
ctx. submit_command ( druid:: commands:: SHOW_OPEN_PANEL . with ( open_dialog_options) ) ;
143
168
}
144
169
145
- fn save_to_file ( data : & mut ApplicationState , ctx : & mut druid:: EventCtx ) {
170
+ fn save_to_file ( data : & mut WindowData , ctx : & mut druid:: EventCtx ) {
146
171
let save_dialog_options = FileDialogOptions :: new ( )
147
172
. allowed_types ( vec ! [ FileSpec :: TEXT ] )
148
173
. default_type ( FileSpec :: TEXT )
@@ -172,21 +197,25 @@ impl Widget<ApplicationState> for ToolBarWidget {
172
197
self . left_buttons . event ( ctx, event, data, env) ;
173
198
self . right_buttons . event ( ctx, event, data, env) ;
174
199
200
+ let win_data = data
201
+ . windows
202
+ . get_mut ( & ctx. window_id ( ) )
203
+ . expect ( "Invalid WindowID" ) ;
175
204
// Prevent the mouse event to be propagated to underlying widgets
176
205
match event {
177
206
Event :: WindowConnected => {
178
- ctx. submit_command ( BUTTON_HIGHLIGHT_COMMAND . with ( data . mode . to_string ( ) ) ) ;
207
+ ctx. submit_command ( BUTTON_HIGHLIGHT_COMMAND . with ( win_data . mode . to_string ( ) ) ) ;
179
208
}
180
209
Event :: Notification ( notification) => {
181
210
if let Some ( name) = notification. get ( BUTTON_HIGHLIGHT_COMMAND ) {
182
211
ctx. submit_command ( BUTTON_HIGHLIGHT_COMMAND . with ( name. to_string ( ) ) ) ;
183
212
}
184
213
}
185
214
Event :: KeyDown ( event) => {
186
- if data . mode != DrawingTools :: Text && event. mods . meta ( ) || event. mods . ctrl ( ) {
215
+ if win_data . mode != DrawingTools :: Text && event. mods . meta ( ) || event. mods . ctrl ( ) {
187
216
match event. code {
188
217
druid:: Code :: KeyS => {
189
- save_to_file ( data , ctx) ;
218
+ save_to_file ( win_data , ctx) ;
190
219
}
191
220
druid:: Code :: KeyO => {
192
221
open_from_file ( ctx) ;
@@ -218,8 +247,16 @@ impl Widget<ApplicationState> for ToolBarWidget {
218
247
data : & ApplicationState ,
219
248
env : & druid:: Env ,
220
249
) {
221
- if old_data. mode != data. mode {
222
- ctx. submit_command ( BUTTON_HIGHLIGHT_COMMAND . with ( data. mode . to_string ( ) ) ) ;
250
+ let win_data = data
251
+ . windows
252
+ . get ( & ctx. window_id ( ) )
253
+ . expect ( "Invalid WindowID" ) ;
254
+ let old_win_data = old_data
255
+ . windows
256
+ . get ( & ctx. window_id ( ) )
257
+ . expect ( "Invalid WindowID" ) ;
258
+ if old_win_data. mode != win_data. mode {
259
+ ctx. submit_command ( BUTTON_HIGHLIGHT_COMMAND . with ( win_data. mode . to_string ( ) ) ) ;
223
260
}
224
261
self . left_buttons . update ( ctx, data, env) ;
225
262
self . right_buttons . update ( ctx, data, env) ;
0 commit comments