@@ -11,7 +11,7 @@ pub fn main() -> iced::Result {
11
11
}
12
12
13
13
struct Example {
14
- panes : pane_grid:: State < Content > ,
14
+ panes : pane_grid:: State < Pane > ,
15
15
panes_created : usize ,
16
16
focus : Option < pane_grid:: Pane > ,
17
17
}
@@ -24,6 +24,7 @@ enum Message {
24
24
Clicked ( pane_grid:: Pane ) ,
25
25
Dragged ( pane_grid:: DragEvent ) ,
26
26
Resized ( pane_grid:: ResizeEvent ) ,
27
+ TogglePin ( pane_grid:: Pane ) ,
27
28
Close ( pane_grid:: Pane ) ,
28
29
CloseFocused ,
29
30
}
@@ -34,7 +35,7 @@ impl Application for Example {
34
35
type Flags = ( ) ;
35
36
36
37
fn new ( _flags : ( ) ) -> ( Self , Command < Message > ) {
37
- let ( panes, _) = pane_grid:: State :: new ( Content :: new ( 0 ) ) ;
38
+ let ( panes, _) = pane_grid:: State :: new ( Pane :: new ( 0 ) ) ;
38
39
39
40
(
40
41
Example {
@@ -60,7 +61,7 @@ impl Application for Example {
60
61
let result = self . panes . split (
61
62
axis,
62
63
& pane,
63
- Content :: new ( self . panes_created ) ,
64
+ Pane :: new ( self . panes_created ) ,
64
65
) ;
65
66
66
67
if let Some ( ( pane, _) ) = result {
@@ -74,7 +75,7 @@ impl Application for Example {
74
75
let result = self . panes . split (
75
76
axis,
76
77
& pane,
77
- Content :: new ( self . panes_created ) ,
78
+ Pane :: new ( self . panes_created ) ,
78
79
) ;
79
80
80
81
if let Some ( ( pane, _) ) = result {
@@ -106,15 +107,27 @@ impl Application for Example {
106
107
self . panes . swap ( & pane, & target) ;
107
108
}
108
109
Message :: Dragged ( _) => { }
110
+ Message :: TogglePin ( pane) => {
111
+ if let Some ( Pane { is_pinned, .. } ) = self . panes . get_mut ( & pane)
112
+ {
113
+ * is_pinned = !* is_pinned;
114
+ }
115
+ }
109
116
Message :: Close ( pane) => {
110
117
if let Some ( ( _, sibling) ) = self . panes . close ( & pane) {
111
118
self . focus = Some ( sibling) ;
112
119
}
113
120
}
114
121
Message :: CloseFocused => {
115
122
if let Some ( pane) = self . focus {
116
- if let Some ( ( _, sibling) ) = self . panes . close ( & pane) {
117
- self . focus = Some ( sibling) ;
123
+ if let Some ( Pane { is_pinned, .. } ) = self . panes . get ( & pane)
124
+ {
125
+ if !is_pinned {
126
+ if let Some ( ( _, sibling) ) = self . panes . close ( & pane)
127
+ {
128
+ self . focus = Some ( sibling) ;
129
+ }
130
+ }
118
131
}
119
132
}
120
133
}
@@ -143,12 +156,20 @@ impl Application for Example {
143
156
let focus = self . focus ;
144
157
let total_panes = self . panes . len ( ) ;
145
158
146
- let pane_grid = PaneGrid :: new ( & mut self . panes , |pane, content| {
147
- let is_focused = focus == Some ( pane) ;
159
+ let pane_grid = PaneGrid :: new ( & mut self . panes , |id, pane| {
160
+ let is_focused = focus == Some ( id) ;
161
+
162
+ let text = if pane. is_pinned { "Unpin" } else { "Pin" } ;
163
+ let pin_button =
164
+ Button :: new ( & mut pane. pin_button , Text :: new ( text) . size ( 14 ) )
165
+ . on_press ( Message :: TogglePin ( id) )
166
+ . style ( style:: Button :: Pin )
167
+ . padding ( 3 ) ;
148
168
149
169
let title = Row :: with_children ( vec ! [
170
+ pin_button. into( ) ,
150
171
Text :: new( "Pane" ) . into( ) ,
151
- Text :: new( content. id. to_string( ) )
172
+ Text :: new( pane . content. id. to_string( ) )
152
173
. color( if is_focused {
153
174
PANE_ID_COLOR_FOCUSED
154
175
} else {
@@ -159,12 +180,17 @@ impl Application for Example {
159
180
. spacing ( 5 ) ;
160
181
161
182
let title_bar = pane_grid:: TitleBar :: new ( title)
183
+ . controls ( pane. controls . view ( id, total_panes, pane. is_pinned ) )
162
184
. padding ( 10 )
163
185
. style ( style:: TitleBar { is_focused } ) ;
164
186
165
- pane_grid:: Content :: new ( content. view ( pane, total_panes) )
166
- . title_bar ( title_bar)
167
- . style ( style:: Pane { is_focused } )
187
+ pane_grid:: Content :: new ( pane. content . view (
188
+ id,
189
+ total_panes,
190
+ pane. is_pinned ,
191
+ ) )
192
+ . title_bar ( title_bar)
193
+ . style ( style:: Pane { is_focused } )
168
194
} )
169
195
. width ( Length :: Fill )
170
196
. height ( Length :: Fill )
@@ -212,6 +238,13 @@ fn handle_hotkey(key_code: keyboard::KeyCode) -> Option<Message> {
212
238
}
213
239
}
214
240
241
+ struct Pane {
242
+ pub is_pinned : bool ,
243
+ pub pin_button : button:: State ,
244
+ pub content : Content ,
245
+ pub controls : Controls ,
246
+ }
247
+
215
248
struct Content {
216
249
id : usize ,
217
250
scroll : scrollable:: State ,
@@ -220,6 +253,21 @@ struct Content {
220
253
close : button:: State ,
221
254
}
222
255
256
+ struct Controls {
257
+ close : button:: State ,
258
+ }
259
+
260
+ impl Pane {
261
+ fn new ( id : usize ) -> Self {
262
+ Self {
263
+ is_pinned : false ,
264
+ pin_button : button:: State :: new ( ) ,
265
+ content : Content :: new ( id) ,
266
+ controls : Controls :: new ( ) ,
267
+ }
268
+ }
269
+ }
270
+
223
271
impl Content {
224
272
fn new ( id : usize ) -> Self {
225
273
Content {
@@ -234,6 +282,7 @@ impl Content {
234
282
& mut self ,
235
283
pane : pane_grid:: Pane ,
236
284
total_panes : usize ,
285
+ is_pinned : bool ,
237
286
) -> Element < Message > {
238
287
let Content {
239
288
scroll,
@@ -273,7 +322,7 @@ impl Content {
273
322
style:: Button :: Primary ,
274
323
) ) ;
275
324
276
- if total_panes > 1 {
325
+ if total_panes > 1 && !is_pinned {
277
326
controls = controls. push ( button (
278
327
close,
279
328
"Close" ,
@@ -297,7 +346,32 @@ impl Content {
297
346
}
298
347
}
299
348
349
+ impl Controls {
350
+ fn new ( ) -> Self {
351
+ Self {
352
+ close : button:: State :: new ( ) ,
353
+ }
354
+ }
355
+
356
+ pub fn view (
357
+ & mut self ,
358
+ pane : pane_grid:: Pane ,
359
+ total_panes : usize ,
360
+ is_pinned : bool ,
361
+ ) -> Element < Message > {
362
+ let mut button =
363
+ Button :: new ( & mut self . close , Text :: new ( "Close" ) . size ( 14 ) )
364
+ . style ( style:: Button :: Control )
365
+ . padding ( 3 ) ;
366
+ if total_panes > 1 && !is_pinned {
367
+ button = button. on_press ( Message :: Close ( pane) ) ;
368
+ }
369
+ button. into ( )
370
+ }
371
+ }
372
+
300
373
mod style {
374
+ use crate :: PANE_ID_COLOR_FOCUSED ;
301
375
use iced:: { button, container, Background , Color , Vector } ;
302
376
303
377
const SURFACE : Color = Color :: from_rgb (
@@ -359,6 +433,8 @@ mod style {
359
433
pub enum Button {
360
434
Primary ,
361
435
Destructive ,
436
+ Control ,
437
+ Pin ,
362
438
}
363
439
364
440
impl button:: StyleSheet for Button {
@@ -368,6 +444,8 @@ mod style {
368
444
Button :: Destructive => {
369
445
( None , Color :: from_rgb8 ( 0xFF , 0x47 , 0x47 ) )
370
446
}
447
+ Button :: Control => ( Some ( PANE_ID_COLOR_FOCUSED ) , Color :: WHITE ) ,
448
+ Button :: Pin => ( Some ( ACTIVE ) , Color :: WHITE ) ,
371
449
} ;
372
450
373
451
button:: Style {
@@ -388,6 +466,8 @@ mod style {
388
466
a : 0.2 ,
389
467
..active. text_color
390
468
} ) ,
469
+ Button :: Control => Some ( PANE_ID_COLOR_FOCUSED ) ,
470
+ Button :: Pin => Some ( HOVERED ) ,
391
471
} ;
392
472
393
473
button:: Style {
0 commit comments