-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathrender.rs
More file actions
3024 lines (2843 loc) · 111 KB
/
render.rs
File metadata and controls
3024 lines (2843 loc) · 111 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#![allow(clippy::too_many_arguments)]
use anyhow::Result;
use crate::backend::wayland::toolbar::format_binding_label;
use crate::draw::{
BLACK, BLUE, Color, EraserKind, FontDescriptor, GREEN, ORANGE, PINK, RED, WHITE, YELLOW,
};
use crate::input::state::PresetFeedbackKind;
use crate::input::{EraserMode, Tool};
use crate::toolbar_icons;
use crate::ui::toolbar::{ToolbarEvent, ToolbarSnapshot};
use crate::util::color_to_name;
use super::events::{HitKind, delay_secs_from_t, delay_t_from_ms};
use super::hit::HitRegion;
use super::layout::ToolbarLayoutSpec;
pub fn render_top_strip(
ctx: &cairo::Context,
width: f64,
height: f64,
snapshot: &ToolbarSnapshot,
hits: &mut Vec<HitRegion>,
hover: Option<(f64, f64)>,
) -> Result<()> {
draw_panel_background(ctx, width, height);
ctx.select_font_face("Sans", cairo::FontSlant::Normal, cairo::FontWeight::Bold);
const TOP_LABEL_FONT_SIZE: f64 = 14.0;
const ICON_TOGGLE_FONT_SIZE: f64 = 12.0;
ctx.set_font_size(TOP_LABEL_FONT_SIZE);
let spec = ToolbarLayoutSpec::new(snapshot);
let use_icons = snapshot.use_icons;
let gap = ToolbarLayoutSpec::TOP_GAP;
let mut x = ToolbarLayoutSpec::TOP_START_X;
let tool_tooltip = |tool: Tool, label: &str| {
let default_hint = match tool {
Tool::Line => Some("Shift+Drag"),
Tool::Rect => Some("Ctrl+Drag"),
Tool::Ellipse => Some("Tab+Drag"),
Tool::Arrow => Some("Ctrl+Shift+Drag"),
_ => None,
};
let binding = match (snapshot.binding_hints.for_tool(tool), default_hint) {
(Some(binding), Some(fallback)) => Some(format!("{}, {}", binding, fallback)),
(Some(binding), None) => Some(binding.to_string()),
(None, Some(fallback)) => Some(fallback.to_string()),
(None, None) => None,
};
format_binding_label(label, binding.as_deref())
};
// Drag handle (left)
let handle_w = ToolbarLayoutSpec::TOP_HANDLE_SIZE;
let handle_h = ToolbarLayoutSpec::TOP_HANDLE_SIZE;
let handle_y = ToolbarLayoutSpec::TOP_HANDLE_Y;
let handle_hover = hover
.map(|(hx, hy)| point_in_rect(hx, hy, x, handle_y, handle_w, handle_h))
.unwrap_or(false);
draw_drag_handle(ctx, x, handle_y, handle_w, handle_h, handle_hover);
hits.push(HitRegion {
rect: (x, handle_y, handle_w, handle_h),
event: ToolbarEvent::MoveTopToolbar { x: 0.0, y: 0.0 },
kind: HitKind::DragMoveTop,
tooltip: Some("Drag toolbar".to_string()),
});
x += handle_w + gap;
type IconFn = fn(&cairo::Context, f64, f64, f64);
let is_simple = snapshot.layout_mode == crate::config::ToolbarLayoutMode::Simple;
let current_shape_tool = match snapshot.tool_override {
Some(Tool::Line) => Some(Tool::Line),
Some(Tool::Rect) => Some(Tool::Rect),
Some(Tool::Ellipse) => Some(Tool::Ellipse),
Some(Tool::Arrow) => Some(Tool::Arrow),
_ => match snapshot.active_tool {
Tool::Line => Some(Tool::Line),
Tool::Rect => Some(Tool::Rect),
Tool::Ellipse => Some(Tool::Ellipse),
Tool::Arrow => Some(Tool::Arrow),
_ => None,
},
};
let shape_icon_tool = current_shape_tool.unwrap_or(Tool::Rect);
let fill_tool_active = matches!(snapshot.tool_override, Some(Tool::Rect | Tool::Ellipse))
|| matches!(snapshot.active_tool, Tool::Rect | Tool::Ellipse);
if use_icons {
let (btn_size, _) = spec.top_button_size();
let y = spec.top_button_y(height);
let icon_size = ToolbarLayoutSpec::TOP_ICON_SIZE;
let fill_h = ToolbarLayoutSpec::TOP_ICON_FILL_HEIGHT;
let mut fill_anchor: Option<(f64, f64)> = None;
let tool_buttons: &[(Tool, IconFn, &str)] = if is_simple {
&[
(
Tool::Select,
toolbar_icons::draw_icon_select as IconFn,
"Select",
),
(Tool::Pen, toolbar_icons::draw_icon_pen as IconFn, "Pen"),
(
Tool::Marker,
toolbar_icons::draw_icon_marker as IconFn,
"Marker",
),
(
Tool::Eraser,
toolbar_icons::draw_icon_eraser as IconFn,
"Eraser",
),
]
} else {
&[
(
Tool::Select,
toolbar_icons::draw_icon_select as IconFn,
"Select",
),
(Tool::Pen, toolbar_icons::draw_icon_pen as IconFn, "Pen"),
(
Tool::Marker,
toolbar_icons::draw_icon_marker as IconFn,
"Marker",
),
(
Tool::Eraser,
toolbar_icons::draw_icon_eraser as IconFn,
"Eraser",
),
(Tool::Line, toolbar_icons::draw_icon_line as IconFn, "Line"),
(Tool::Rect, toolbar_icons::draw_icon_rect as IconFn, "Rect"),
(
Tool::Ellipse,
toolbar_icons::draw_icon_circle as IconFn,
"Circle",
),
(
Tool::Arrow,
toolbar_icons::draw_icon_arrow as IconFn,
"Arrow",
),
]
};
let mut rect_x = None;
let mut circle_end_x = None;
for (tool, icon_fn, label) in tool_buttons {
if *tool == Tool::Rect {
rect_x = Some(x);
}
if *tool == Tool::Ellipse {
circle_end_x = Some(x + btn_size);
}
let is_active = snapshot.active_tool == *tool || snapshot.tool_override == Some(*tool);
let is_hover = hover
.map(|(hx, hy)| point_in_rect(hx, hy, x, y, btn_size, btn_size))
.unwrap_or(false);
draw_button(ctx, x, y, btn_size, btn_size, is_active, is_hover);
set_icon_color(ctx, is_hover);
let icon_x = x + (btn_size - icon_size) / 2.0;
let icon_y = y + (btn_size - icon_size) / 2.0;
icon_fn(ctx, icon_x, icon_y, icon_size);
let tooltip = tool_tooltip(*tool, label);
hits.push(HitRegion {
rect: (x, y, btn_size, btn_size),
event: ToolbarEvent::SelectTool(*tool),
kind: HitKind::Click,
tooltip: Some(tooltip),
});
x += btn_size + gap;
}
if is_simple {
let shapes_active = snapshot.shape_picker_open || current_shape_tool.is_some();
let shapes_hover = hover
.map(|(hx, hy)| point_in_rect(hx, hy, x, y, btn_size, btn_size))
.unwrap_or(false);
draw_button(ctx, x, y, btn_size, btn_size, shapes_active, shapes_hover);
set_icon_color(ctx, shapes_hover);
let icon_x = x + (btn_size - icon_size) / 2.0;
let icon_y = y + (btn_size - icon_size) / 2.0;
match shape_icon_tool {
Tool::Line => toolbar_icons::draw_icon_line(ctx, icon_x, icon_y, icon_size),
Tool::Rect => toolbar_icons::draw_icon_rect(ctx, icon_x, icon_y, icon_size),
Tool::Ellipse => toolbar_icons::draw_icon_circle(ctx, icon_x, icon_y, icon_size),
Tool::Arrow => toolbar_icons::draw_icon_arrow(ctx, icon_x, icon_y, icon_size),
_ => toolbar_icons::draw_icon_rect(ctx, icon_x, icon_y, icon_size),
}
hits.push(HitRegion {
rect: (x, y, btn_size, btn_size),
event: ToolbarEvent::ToggleShapePicker(!snapshot.shape_picker_open),
kind: HitKind::Click,
tooltip: Some("Shapes".to_string()),
});
if fill_tool_active && !snapshot.shape_picker_open {
fill_anchor = Some((x, btn_size));
}
x += btn_size + gap;
} else if let (Some(rect_x), Some(circle_end_x)) = (rect_x, circle_end_x) {
fill_anchor = Some((rect_x, circle_end_x - rect_x));
}
if fill_tool_active
&& !(is_simple && snapshot.shape_picker_open)
&& let Some((fill_x, fill_w)) = fill_anchor
{
let fill_y = y + btn_size + ToolbarLayoutSpec::TOP_ICON_FILL_OFFSET;
let fill_hover = hover
.map(|(hx, hy)| point_in_rect(hx, hy, fill_x, fill_y, fill_w, fill_h))
.unwrap_or(false);
draw_mini_checkbox(
ctx,
fill_x,
fill_y,
fill_w,
fill_h,
snapshot.fill_enabled,
fill_hover,
"Fill",
);
hits.push(HitRegion {
rect: (fill_x, fill_y, fill_w, fill_h),
event: ToolbarEvent::ToggleFill(!snapshot.fill_enabled),
kind: HitKind::Click,
tooltip: Some(format_binding_label(
"Fill",
snapshot.binding_hints.fill.as_deref(),
)),
});
}
let is_hover = hover
.map(|(hx, hy)| point_in_rect(hx, hy, x, y, btn_size, btn_size))
.unwrap_or(false);
draw_button(
ctx,
x,
y,
btn_size,
btn_size,
snapshot.text_active,
is_hover,
);
set_icon_color(ctx, is_hover);
toolbar_icons::draw_icon_text(
ctx,
x + (btn_size - icon_size) / 2.0,
y + (btn_size - icon_size) / 2.0,
icon_size,
);
hits.push(HitRegion {
rect: (x, y, btn_size, btn_size),
event: ToolbarEvent::EnterTextMode,
kind: HitKind::Click,
tooltip: Some(format_binding_label(
"Text",
snapshot.binding_hints.text.as_deref(),
)),
});
x += btn_size + gap;
let note_hover = hover
.map(|(hx, hy)| point_in_rect(hx, hy, x, y, btn_size, btn_size))
.unwrap_or(false);
draw_button(
ctx,
x,
y,
btn_size,
btn_size,
snapshot.note_active,
note_hover,
);
set_icon_color(ctx, note_hover);
toolbar_icons::draw_icon_note(
ctx,
x + (btn_size - icon_size) / 2.0,
y + (btn_size - icon_size) / 2.0,
icon_size,
);
hits.push(HitRegion {
rect: (x, y, btn_size, btn_size),
event: ToolbarEvent::EnterStickyNoteMode,
kind: HitKind::Click,
tooltip: Some(format_binding_label(
"Note",
snapshot.binding_hints.note.as_deref(),
)),
});
x += btn_size + gap;
if !is_simple {
let clear_hover = hover
.map(|(hx, hy)| point_in_rect(hx, hy, x, y, btn_size, btn_size))
.unwrap_or(false);
draw_button(ctx, x, y, btn_size, btn_size, false, clear_hover);
set_icon_color(ctx, clear_hover);
toolbar_icons::draw_icon_clear(
ctx,
x + (btn_size - icon_size) / 2.0,
y + (btn_size - icon_size) / 2.0,
icon_size,
);
hits.push(HitRegion {
rect: (x, y, btn_size, btn_size),
event: ToolbarEvent::ClearCanvas,
kind: HitKind::Click,
tooltip: Some(format_binding_label(
"Clear",
snapshot.binding_hints.clear.as_deref(),
)),
});
x += btn_size + gap;
let highlight_hover = hover
.map(|(hx, hy)| point_in_rect(hx, hy, x, y, btn_size, btn_size))
.unwrap_or(false);
draw_button(
ctx,
x,
y,
btn_size,
btn_size,
snapshot.any_highlight_active,
highlight_hover,
);
set_icon_color(ctx, highlight_hover);
toolbar_icons::draw_icon_highlight(
ctx,
x + (btn_size - icon_size) / 2.0,
y + (btn_size - icon_size) / 2.0,
icon_size,
);
hits.push(HitRegion {
rect: (x, y, btn_size, btn_size),
event: ToolbarEvent::ToggleAllHighlight(!snapshot.any_highlight_active),
kind: HitKind::Click,
tooltip: Some(format_binding_label(
"Click highlight",
snapshot.binding_hints.toggle_highlight.as_deref(),
)),
});
x += btn_size + gap;
}
let icons_w = ToolbarLayoutSpec::TOP_TOGGLE_WIDTH;
let icons_hover = hover
.map(|(hx, hy)| point_in_rect(hx, hy, x, y, icons_w, btn_size))
.unwrap_or(false);
ctx.set_font_size(ICON_TOGGLE_FONT_SIZE);
draw_checkbox(ctx, x, y, icons_w, btn_size, true, icons_hover, "Icons");
ctx.set_font_size(TOP_LABEL_FONT_SIZE);
hits.push(HitRegion {
rect: (x, y, icons_w, btn_size),
event: ToolbarEvent::ToggleIconMode(false),
kind: HitKind::Click,
tooltip: None,
});
if is_simple && snapshot.shape_picker_open {
let shape_y = y + btn_size + ToolbarLayoutSpec::TOP_SHAPE_ROW_GAP;
let mut shape_x = ToolbarLayoutSpec::TOP_START_X + handle_w + gap;
let shapes: &[(Tool, IconFn, &str)] = &[
(Tool::Line, toolbar_icons::draw_icon_line as IconFn, "Line"),
(Tool::Rect, toolbar_icons::draw_icon_rect as IconFn, "Rect"),
(
Tool::Ellipse,
toolbar_icons::draw_icon_circle as IconFn,
"Circle",
),
(
Tool::Arrow,
toolbar_icons::draw_icon_arrow as IconFn,
"Arrow",
),
];
for (tool, icon_fn, label) in shapes {
let is_active =
snapshot.active_tool == *tool || snapshot.tool_override == Some(*tool);
let is_hover = hover
.map(|(hx, hy)| point_in_rect(hx, hy, shape_x, shape_y, btn_size, btn_size))
.unwrap_or(false);
draw_button(
ctx, shape_x, shape_y, btn_size, btn_size, is_active, is_hover,
);
set_icon_color(ctx, is_hover);
let icon_x = shape_x + (btn_size - icon_size) / 2.0;
let icon_y = shape_y + (btn_size - icon_size) / 2.0;
icon_fn(ctx, icon_x, icon_y, icon_size);
let tooltip = tool_tooltip(*tool, label);
hits.push(HitRegion {
rect: (shape_x, shape_y, btn_size, btn_size),
event: ToolbarEvent::SelectTool(*tool),
kind: HitKind::Click,
tooltip: Some(tooltip),
});
shape_x += btn_size + gap;
}
}
} else {
let (btn_w, btn_h) = spec.top_button_size();
let y = spec.top_button_y(height);
let tool_buttons: &[(Tool, &str)] = if is_simple {
&[
(Tool::Select, "Select"),
(Tool::Pen, "Pen"),
(Tool::Marker, "Marker"),
(Tool::Eraser, "Eraser"),
]
} else {
&[
(Tool::Select, "Select"),
(Tool::Pen, "Pen"),
(Tool::Marker, "Marker"),
(Tool::Eraser, "Eraser"),
(Tool::Line, "Line"),
(Tool::Rect, "Rect"),
(Tool::Ellipse, "Circle"),
(Tool::Arrow, "Arrow"),
]
};
for (tool, label) in tool_buttons {
let is_active = snapshot.active_tool == *tool || snapshot.tool_override == Some(*tool);
let is_hover = hover
.map(|(hx, hy)| point_in_rect(hx, hy, x, y, btn_w, btn_h))
.unwrap_or(false);
draw_button(ctx, x, y, btn_w, btn_h, is_active, is_hover);
draw_label_center(ctx, x, y, btn_w, btn_h, label);
let tooltip = tool_tooltip(*tool, label);
hits.push(HitRegion {
rect: (x, y, btn_w, btn_h),
event: ToolbarEvent::SelectTool(*tool),
kind: HitKind::Click,
tooltip: Some(tooltip),
});
x += btn_w + gap;
}
if is_simple {
let shapes_active = snapshot.shape_picker_open || current_shape_tool.is_some();
let shapes_hover = hover
.map(|(hx, hy)| point_in_rect(hx, hy, x, y, btn_w, btn_h))
.unwrap_or(false);
draw_button(ctx, x, y, btn_w, btn_h, shapes_active, shapes_hover);
draw_label_center(ctx, x, y, btn_w, btn_h, "Shapes");
hits.push(HitRegion {
rect: (x, y, btn_w, btn_h),
event: ToolbarEvent::ToggleShapePicker(!snapshot.shape_picker_open),
kind: HitKind::Click,
tooltip: Some("Shapes".to_string()),
});
x += btn_w + gap;
}
if fill_tool_active {
let fill_w = ToolbarLayoutSpec::TOP_TEXT_FILL_W;
let fill_hover = hover
.map(|(hx, hy)| point_in_rect(hx, hy, x, y, fill_w, btn_h))
.unwrap_or(false);
draw_checkbox(
ctx,
x,
y,
fill_w,
btn_h,
snapshot.fill_enabled,
fill_hover,
"Fill",
);
hits.push(HitRegion {
rect: (x, y, fill_w, btn_h),
event: ToolbarEvent::ToggleFill(!snapshot.fill_enabled),
kind: HitKind::Click,
tooltip: Some(format_binding_label(
"Fill",
snapshot.binding_hints.fill.as_deref(),
)),
});
x += fill_w + gap;
}
let is_hover = hover
.map(|(hx, hy)| point_in_rect(hx, hy, x, y, btn_w, btn_h))
.unwrap_or(false);
draw_button(ctx, x, y, btn_w, btn_h, snapshot.text_active, is_hover);
draw_label_center(ctx, x, y, btn_w, btn_h, "Text");
hits.push(HitRegion {
rect: (x, y, btn_w, btn_h),
event: ToolbarEvent::EnterTextMode,
kind: HitKind::Click,
tooltip: Some(format_binding_label(
"Text",
snapshot.binding_hints.text.as_deref(),
)),
});
x += btn_w + gap;
let note_hover = hover
.map(|(hx, hy)| point_in_rect(hx, hy, x, y, btn_w, btn_h))
.unwrap_or(false);
draw_button(ctx, x, y, btn_w, btn_h, snapshot.note_active, note_hover);
draw_label_center(ctx, x, y, btn_w, btn_h, "Note");
hits.push(HitRegion {
rect: (x, y, btn_w, btn_h),
event: ToolbarEvent::EnterStickyNoteMode,
kind: HitKind::Click,
tooltip: Some(format_binding_label(
"Note",
snapshot.binding_hints.note.as_deref(),
)),
});
x += btn_w + gap;
if !is_simple {
let clear_hover = hover
.map(|(hx, hy)| point_in_rect(hx, hy, x, y, btn_w, btn_h))
.unwrap_or(false);
draw_button(ctx, x, y, btn_w, btn_h, false, clear_hover);
draw_label_center(ctx, x, y, btn_w, btn_h, "Clear");
hits.push(HitRegion {
rect: (x, y, btn_w, btn_h),
event: ToolbarEvent::ClearCanvas,
kind: HitKind::Click,
tooltip: Some(format_binding_label(
"Clear",
snapshot.binding_hints.clear.as_deref(),
)),
});
x += btn_w + gap;
}
let icons_w = ToolbarLayoutSpec::TOP_TOGGLE_WIDTH;
let icons_hover = hover
.map(|(hx, hy)| point_in_rect(hx, hy, x, y, icons_w, btn_h))
.unwrap_or(false);
ctx.set_font_size(ICON_TOGGLE_FONT_SIZE);
draw_checkbox(ctx, x, y, icons_w, btn_h, false, icons_hover, "Icons");
ctx.set_font_size(TOP_LABEL_FONT_SIZE);
hits.push(HitRegion {
rect: (x, y, icons_w, btn_h),
event: ToolbarEvent::ToggleIconMode(true),
kind: HitKind::Click,
tooltip: None,
});
if is_simple && snapshot.shape_picker_open {
let shape_y = y + btn_h + ToolbarLayoutSpec::TOP_SHAPE_ROW_GAP;
let mut shape_x = ToolbarLayoutSpec::TOP_START_X + handle_w + gap;
let shapes: &[(Tool, &str)] = &[
(Tool::Line, "Line"),
(Tool::Rect, "Rect"),
(Tool::Ellipse, "Circle"),
(Tool::Arrow, "Arrow"),
];
for (tool, label) in shapes {
let is_active =
snapshot.active_tool == *tool || snapshot.tool_override == Some(*tool);
let is_hover = hover
.map(|(hx, hy)| point_in_rect(hx, hy, shape_x, shape_y, btn_w, btn_h))
.unwrap_or(false);
draw_button(ctx, shape_x, shape_y, btn_w, btn_h, is_active, is_hover);
draw_label_center(ctx, shape_x, shape_y, btn_w, btn_h, label);
let tooltip = tool_tooltip(*tool, label);
hits.push(HitRegion {
rect: (shape_x, shape_y, btn_w, btn_h),
event: ToolbarEvent::SelectTool(*tool),
kind: HitKind::Click,
tooltip: Some(tooltip),
});
shape_x += btn_w + gap;
}
}
}
let btn_size = ToolbarLayoutSpec::TOP_PIN_BUTTON_SIZE;
let btn_y = spec.top_pin_button_y(height);
let pin_x = spec.top_pin_x(width);
let pin_hover = hover
.map(|(hx, hy)| point_in_rect(hx, hy, pin_x, btn_y, btn_size, btn_size))
.unwrap_or(false);
draw_pin_button(ctx, pin_x, btn_y, btn_size, snapshot.top_pinned, pin_hover);
hits.push(HitRegion {
rect: (pin_x, btn_y, btn_size, btn_size),
event: ToolbarEvent::PinTopToolbar(!snapshot.top_pinned),
kind: HitKind::Click,
tooltip: Some(if snapshot.top_pinned {
"Unpin".to_string()
} else {
"Pin".to_string()
}),
});
let close_x = spec.top_close_x(width);
let close_hover = hover
.map(|(hx, hy)| point_in_rect(hx, hy, close_x, btn_y, btn_size, btn_size))
.unwrap_or(false);
draw_close_button(ctx, close_x, btn_y, btn_size, close_hover);
hits.push(HitRegion {
rect: (close_x, btn_y, btn_size, btn_size),
event: ToolbarEvent::CloseTopToolbar,
kind: HitKind::Click,
tooltip: Some("Close".to_string()),
});
draw_tooltip(ctx, hits, hover, width, false);
Ok(())
}
pub fn render_side_palette(
ctx: &cairo::Context,
width: f64,
_height: f64,
snapshot: &ToolbarSnapshot,
hits: &mut Vec<HitRegion>,
hover: Option<(f64, f64)>,
) -> Result<()> {
draw_panel_background(ctx, width, _height);
let spec = ToolbarLayoutSpec::new(snapshot);
let mut y = ToolbarLayoutSpec::SIDE_TOP_PADDING;
let x = ToolbarLayoutSpec::SIDE_START_X;
let use_icons = snapshot.use_icons;
ctx.select_font_face("Sans", cairo::FontSlant::Normal, cairo::FontWeight::Bold);
ctx.set_font_size(13.0);
let btn_size = ToolbarLayoutSpec::SIDE_HEADER_BUTTON_SIZE;
let handle_w = ToolbarLayoutSpec::SIDE_HEADER_HANDLE_SIZE;
let handle_h = ToolbarLayoutSpec::SIDE_HEADER_HANDLE_SIZE;
// Place handle above the header row to avoid widening the palette.
let handle_hover = hover
.map(|(hx, hy)| point_in_rect(hx, hy, x, y, handle_w, handle_h))
.unwrap_or(false);
draw_drag_handle(ctx, x, y, handle_w, handle_h, handle_hover);
hits.push(HitRegion {
rect: (x, y, handle_w, handle_h),
event: ToolbarEvent::MoveSideToolbar { x: 0.0, y: 0.0 },
kind: HitKind::DragMoveSide,
tooltip: Some("Drag toolbar".to_string()),
});
let header_y = spec.side_header_y();
let icons_w = ToolbarLayoutSpec::SIDE_HEADER_TOGGLE_WIDTH;
let icons_h = btn_size;
let icons_hover = hover
.map(|(hx, hy)| point_in_rect(hx, hy, x, header_y, icons_w, icons_h))
.unwrap_or(false);
draw_checkbox(
ctx,
x,
header_y,
icons_w,
icons_h,
use_icons,
icons_hover,
"Icons",
);
hits.push(HitRegion {
rect: (x, header_y, icons_w, icons_h),
event: ToolbarEvent::ToggleIconMode(!use_icons),
kind: HitKind::Click,
tooltip: None,
});
let mode_w = ToolbarLayoutSpec::SIDE_HEADER_MODE_WIDTH;
let mode_x = x + icons_w + ToolbarLayoutSpec::SIDE_HEADER_MODE_GAP;
let mode_hover = hover
.map(|(hx, hy)| point_in_rect(hx, hy, mode_x, header_y, mode_w, icons_h))
.unwrap_or(false);
draw_button(ctx, mode_x, header_y, mode_w, icons_h, false, mode_hover);
let mode_label = match snapshot.layout_mode {
crate::config::ToolbarLayoutMode::Simple => "Mode: S",
crate::config::ToolbarLayoutMode::Regular => "Mode: R",
crate::config::ToolbarLayoutMode::Advanced => "Mode: A",
};
draw_label_center(ctx, mode_x, header_y, mode_w, icons_h, mode_label);
let next_mode = snapshot.layout_mode.next();
let mode_tooltip = format!(
"Mode: S/R/A = {}/{}/{}",
crate::config::ToolbarLayoutMode::Simple.label(),
crate::config::ToolbarLayoutMode::Regular.label(),
crate::config::ToolbarLayoutMode::Advanced.label(),
);
hits.push(HitRegion {
rect: (mode_x, header_y, mode_w, icons_h),
event: ToolbarEvent::SetToolbarLayoutMode(next_mode),
kind: HitKind::Click,
tooltip: Some(mode_tooltip),
});
let (pin_x, close_x, header_btn_y) = spec.side_header_button_positions(width);
let pin_hover = hover
.map(|(hx, hy)| point_in_rect(hx, hy, pin_x, header_btn_y, btn_size, btn_size))
.unwrap_or(false);
draw_pin_button(
ctx,
pin_x,
header_btn_y,
btn_size,
snapshot.side_pinned,
pin_hover,
);
hits.push(HitRegion {
rect: (pin_x, header_btn_y, btn_size, btn_size),
event: ToolbarEvent::PinSideToolbar(!snapshot.side_pinned),
kind: HitKind::Click,
tooltip: Some(if snapshot.side_pinned {
"Unpin".to_string()
} else {
"Pin".to_string()
}),
});
let close_hover = hover
.map(|(hx, hy)| point_in_rect(hx, hy, close_x, header_btn_y, btn_size, btn_size))
.unwrap_or(false);
draw_close_button(ctx, close_x, header_btn_y, btn_size, close_hover);
hits.push(HitRegion {
rect: (close_x, header_btn_y, btn_size, btn_size),
event: ToolbarEvent::CloseSideToolbar,
kind: HitKind::Click,
tooltip: Some("Close".to_string()),
});
y = spec.side_content_start_y();
let card_x = spec.side_card_x();
let card_w = spec.side_card_width(width);
let content_width = spec.side_content_width(width);
let section_gap = ToolbarLayoutSpec::SIDE_SECTION_GAP;
let mut hover_preset_color: Option<Color> = None;
let show_text_controls =
snapshot.text_active || snapshot.note_active || snapshot.show_text_controls;
let basic_colors: &[(Color, &str)] = &[
(RED, "Red"),
(GREEN, "Green"),
(BLUE, "Blue"),
(YELLOW, "Yellow"),
(WHITE, "White"),
(BLACK, "Black"),
];
let extended_colors: &[(Color, &str)] = &[
(ORANGE, "Orange"),
(PINK, "Pink"),
(
Color {
r: 0.0,
g: 1.0,
b: 1.0,
a: 1.0,
},
"Cyan",
),
(
Color {
r: 0.6,
g: 0.4,
b: 0.8,
a: 1.0,
},
"Purple",
),
(
Color {
r: 0.4,
g: 0.4,
b: 0.4,
a: 1.0,
},
"Gray",
),
];
let swatch = ToolbarLayoutSpec::SIDE_COLOR_SWATCH;
let swatch_gap = ToolbarLayoutSpec::SIDE_COLOR_SWATCH_GAP;
let picker_h = ToolbarLayoutSpec::SIDE_COLOR_PICKER_INPUT_HEIGHT;
let colors_card_h = spec.side_colors_height(snapshot);
draw_group_card(ctx, card_x, y, card_w, colors_card_h);
draw_section_label(
ctx,
x,
y + ToolbarLayoutSpec::SIDE_SECTION_LABEL_OFFSET_Y,
"Colors",
);
let picker_y = y + ToolbarLayoutSpec::SIDE_COLOR_PICKER_OFFSET_Y;
let picker_w = content_width;
draw_color_picker(ctx, x, picker_y, picker_w, picker_h);
hits.push(HitRegion {
rect: (x, picker_y, picker_w, picker_h),
event: ToolbarEvent::SetColor(snapshot.color),
kind: HitKind::PickColor {
x,
y: picker_y,
w: picker_w,
h: picker_h + if snapshot.show_more_colors { 30.0 } else { 0.0 },
},
tooltip: None,
});
let mut cx = x;
let mut row_y = picker_y + picker_h + 8.0;
for (color, _name) in basic_colors {
draw_swatch(ctx, cx, row_y, swatch, *color, *color == snapshot.color);
hits.push(HitRegion {
rect: (cx, row_y, swatch, swatch),
event: ToolbarEvent::SetColor(*color),
kind: HitKind::Click,
tooltip: None,
});
cx += swatch + swatch_gap;
}
if !snapshot.show_more_colors {
let plus_btn_hover = hover
.map(|(hx, hy)| point_in_rect(hx, hy, cx, row_y, swatch, swatch))
.unwrap_or(false);
draw_button(ctx, cx, row_y, swatch, swatch, false, plus_btn_hover);
set_icon_color(ctx, plus_btn_hover);
toolbar_icons::draw_icon_plus(
ctx,
cx + (swatch - 14.0) / 2.0,
row_y + (swatch - 14.0) / 2.0,
14.0,
);
hits.push(HitRegion {
rect: (cx, row_y, swatch, swatch),
event: ToolbarEvent::ToggleMoreColors(true),
kind: HitKind::Click,
tooltip: Some("More colors".to_string()),
});
}
row_y += swatch + swatch_gap;
if snapshot.show_more_colors {
cx = x;
for (color, _name) in extended_colors {
draw_swatch(ctx, cx, row_y, swatch, *color, *color == snapshot.color);
hits.push(HitRegion {
rect: (cx, row_y, swatch, swatch),
event: ToolbarEvent::SetColor(*color),
kind: HitKind::Click,
tooltip: None,
});
cx += swatch + swatch_gap;
}
let minus_btn_hover = hover
.map(|(hx, hy)| point_in_rect(hx, hy, cx, row_y, swatch, swatch))
.unwrap_or(false);
draw_button(ctx, cx, row_y, swatch, swatch, false, minus_btn_hover);
set_icon_color(ctx, minus_btn_hover);
toolbar_icons::draw_icon_minus(
ctx,
cx + (swatch - 14.0) / 2.0,
row_y + (swatch - 14.0) / 2.0,
14.0,
);
hits.push(HitRegion {
rect: (cx, row_y, swatch, swatch),
event: ToolbarEvent::ToggleMoreColors(false),
kind: HitKind::Click,
tooltip: Some("Hide colors".to_string()),
});
}
y += colors_card_h + section_gap;
let slot_count = snapshot.preset_slot_count.min(snapshot.presets.len());
if snapshot.show_presets && slot_count > 0 {
let presets_card_h = ToolbarLayoutSpec::SIDE_PRESET_CARD_HEIGHT;
draw_group_card(ctx, card_x, y, card_w, presets_card_h);
draw_section_label(
ctx,
x,
y + ToolbarLayoutSpec::SIDE_SECTION_LABEL_OFFSET_Y,
"Presets",
);
let apply_hint = {
let mut uses_digit_bindings = true;
for slot in 1..=slot_count {
let expected = slot.to_string();
if snapshot.binding_hints.apply_preset(slot) != Some(expected.as_str()) {
uses_digit_bindings = false;
break;
}
}
if uses_digit_bindings {
Some(format!("Keys 1-{} apply", slot_count))
} else {
Some("Keys apply presets".to_string())
}
};
if let Some(hint) = apply_hint {
ctx.select_font_face("Sans", cairo::FontSlant::Normal, cairo::FontWeight::Normal);
ctx.set_font_size(10.0);
if let Ok(ext) = ctx.text_extents(&hint) {
let hint_x = card_x + card_w - ext.width() - 8.0 - ext.x_bearing();
let hint_y = y + ToolbarLayoutSpec::SIDE_SECTION_LABEL_OFFSET_Y;
ctx.set_source_rgba(0.7, 0.7, 0.75, 0.8);
ctx.move_to(hint_x, hint_y);
let _ = ctx.show_text(&hint);
}
ctx.select_font_face("Sans", cairo::FontSlant::Normal, cairo::FontWeight::Bold);
ctx.set_font_size(13.0);
}
let slot_size = ToolbarLayoutSpec::SIDE_PRESET_SLOT_SIZE;
let slot_gap = ToolbarLayoutSpec::SIDE_PRESET_SLOT_GAP;
let slot_row_y = y + ToolbarLayoutSpec::SIDE_PRESET_ROW_OFFSET_Y;
let action_row_y = slot_row_y + slot_size + ToolbarLayoutSpec::SIDE_PRESET_ACTION_GAP;
let action_h = ToolbarLayoutSpec::SIDE_PRESET_ACTION_HEIGHT;
let action_gap = ToolbarLayoutSpec::SIDE_PRESET_ACTION_BUTTON_GAP;
let action_w = (slot_size - action_gap) / 2.0;
let action_icon = (action_h * 0.6).round();
let icon_size = (slot_size * 0.45).round();
let swatch_size = (slot_size * 0.35).round();
let number_box = (slot_size * 0.4).round();
let keycap_pad = (slot_size * 0.1).round().max(3.0);
let keycap_radius = (number_box * 0.25).max(3.0);
let draw_keycap = |ctx: &cairo::Context, key_x: f64, key_y: f64, label: &str, active| {
let (bg_alpha, border_alpha, text_alpha) = if active {
(0.75, 0.55, 0.95)
} else {
(0.4, 0.35, 0.6)
};
ctx.set_source_rgba(0.12, 0.12, 0.18, bg_alpha);
draw_round_rect(ctx, key_x, key_y, number_box, number_box, keycap_radius);
let _ = ctx.fill();
ctx.set_source_rgba(1.0, 1.0, 1.0, border_alpha);
ctx.set_line_width(1.0);
draw_round_rect(ctx, key_x, key_y, number_box, number_box, keycap_radius);
let _ = ctx.stroke();
ctx.set_font_size(11.0);
draw_label_center_color(
ctx,
key_x,
key_y,
number_box,
number_box,
label,
(1.0, 1.0, 1.0, text_alpha),
);
ctx.set_font_size(13.0);
};
let tool_label = |tool: Tool| match tool {
Tool::Select => "Select",
Tool::Pen => "Pen",
Tool::Line => "Line",
Tool::Rect => "Rect",
Tool::Ellipse => "Circle",
Tool::Arrow => "Arrow",
Tool::Marker => "Marker",
Tool::Highlight => "Highlight",
Tool::Eraser => "Eraser",
};
let px_label = |value: f64| {
if (value - value.round()).abs() < 0.05 {
format!("{:.0}px", value)
} else {
format!("{:.1}px", value)
}
};
let angle_label = |value: f64| {
if (value - value.round()).abs() < 0.05 {
format!("{:.0}deg", value)
} else {
format!("{:.1}deg", value)
}
};
let on_off = |value: bool| if value { "on" } else { "off" };
let eraser_kind_label = |kind: EraserKind| match kind {
EraserKind::Circle => "circle",
EraserKind::Rect => "rect",
};
let eraser_mode_label = |mode: EraserMode| match mode {
EraserMode::Brush => "brush",
EraserMode::Stroke => "stroke",
};
let truncate_label = |value: &str, max_chars: usize| {
if value.chars().count() <= max_chars {
value.to_string()
} else {
let mut truncated = value
.chars()
.take(max_chars.saturating_sub(3))
.collect::<String>();
truncated.push_str("...");