shapedwindow_taskbar/
shapedwindow_taskbar.rs

1use fltk::{prelude::*, *};
2
3/*
4   Event handling for win and dock_win can be changed.
5
6   In fn prep_shape(), if you want a different shape as your background e.g a pie (circle),
7   change draw::draw_rounded_rectf(0, 0, w, h, 16) to your desired background e.g draw::pie
8                                   x, y, w, h, r
9   * DO NOT CHANGE ANYTHING ELSE IN THE prep_shape() FUNCTION *
10
11   .center_screen(); -- Not a requirement
12   .with_size(1, 1); -- If you use .center_screen(), (x, y) MUST be >0 otherwise a panic!() will be thrown
13   win.set_color(Color::from_rgb(25, 26, 55)); -- Can be refactored to any color, no requirement e.g enums::Color::White
14   button::Button::default(); -- Not a requirement, remove if need be and import your own Widgets
15*/
16
17fn main() {
18    let app = app::App::default();
19
20    // Act as the application in the taskbar (scroll to event handling)
21    let mut dock_win = window::Window::default()
22        .with_size(1, 1) // So we can place it at the center of the screen (needs a size >0 to be centered)
23        .with_label("TestApplication")
24        .center_screen();
25    dock_win.size_range(0, 0, 0, 0);
26    dock_win.make_resizable(false);
27
28    dock_win.show();
29    dock_win.end();
30
31    let mut win = window::Window::default()
32        .with_size(900, 500)
33        .with_label("TestApplication")
34        .center_screen();
35    win.set_color(enums::Color::from_rgb(26, 25, 55));
36
37    let mut but = button::Button::default()
38        .with_label("Button")
39        .with_size(80, 80)
40        .center_of_parent();
41    but.set_frame(enums::FrameType::OFlatFrame);
42    but.set_color(enums::Color::Cyan);
43    but.clear_visible_focus();
44    but.set_callback(|_| println!("Clicked"));
45
46    win.show();
47    win.end();
48
49    let win_shape = prep_shape(win.w(), win.h());
50
51    // Called after showing window
52    win.set_shape(Some(win_shape));
53
54    win.handle({
55        let mut x = 0;
56        let mut y = 0;
57        let mut dock_win = dock_win.clone();
58        move |wself, event| match event {
59            enums::Event::Push => {
60                let coords = app::event_coords();
61                x = coords.0;
62                y = coords.1;
63
64                true
65            }
66            enums::Event::Drag => {
67                wself.set_pos(app::event_x_root() - x, app::event_y_root() - y);
68
69                // Changing dock window position so it's close enough to the center of the application (not "visible" to user)
70                dock_win.set_pos(wself.x() + (wself.w() / 2), wself.y() + (wself.w() / 2));
71
72                true
73            }
74            enums::Event::Close => {
75                app.quit();
76
77                true
78            }
79            enums::Event::Hide => {
80                app.quit();
81
82                true
83            }
84            _ => false,
85        }
86    });
87
88    // Make main window appear when "opened" via Alt+Tab or Taskbar
89    dock_win.handle({
90        let mut win = win.clone();
91        move |_wself, event| match event {
92            enums::Event::Focus => {
93                let win_shape = prep_shape(win.w(), win.h());
94
95                win.show();
96                win.set_shape(Some(win_shape));
97
98                true
99            }
100            enums::Event::Hide => {
101                win.hide();
102
103                true
104            }
105            enums::Event::Close => {
106                app.quit();
107
108                true
109            }
110            _ => false,
111        }
112    });
113
114    app.run().unwrap();
115}
116
117fn prep_shape(w: i32, h: i32) -> image::RgbImage {
118    let surf = surface::ImageSurface::new(w, h, false);
119
120    surface::ImageSurface::push_current(&surf);
121
122    draw::set_draw_color(enums::Color::Black);
123    draw::draw_rectf(-1, -1, w + 2, h + 2);
124
125    draw::set_draw_color(enums::Color::White);
126    draw::draw_rounded_rectf(0, 0, w, h, 16);
127
128    let img = surf.image().unwrap();
129
130    surface::ImageSurface::pop_current();
131
132    img
133}