Mercurial > tablet-emu
annotate src/gtk.rs @ 15:d103f7cca0bd
Update to GTK 4.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Thu, 26 Aug 2021 19:34:40 +0200 |
parents | adab13145994 |
children | 478cf2a7d577 |
rev | line source |
---|---|
13
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
1 // Tablet emulator, for people who don’t own one |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
2 // Copyright © 2020 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
3 // |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
4 // This program is free software: you can redistribute it and/or modify |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
5 // it under the terms of the GNU Affero General Public License as published by |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
6 // the Free Software Foundation, either version 3 of the License, or |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
7 // (at your option) any later version. |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
8 // |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
9 // This program is distributed in the hope that it will be useful, |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
12 // GNU Affero General Public License for more details. |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
13 // |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
14 // You should have received a copy of the GNU Affero General Public License |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
15 // along with this program. If not, see <https://www.gnu.org/licenses/>. |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
16 |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
17 use gio::prelude::*; |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
18 use glib::clone; |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
19 use gtk::prelude::*; |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
20 |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
21 use std::io::ErrorKind; |
14
adab13145994
Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
13
diff
changeset
|
22 use std::sync::{Arc, Mutex}; |
13
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
23 use input_linux::Key; |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
24 |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
25 use crate::{ |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
26 state::State, |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
27 DEFAULT_WIDTH, DEFAULT_HEIGHT, |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
28 }; |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
29 |
15
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
30 fn build_main_menu(app: >k::Application) { |
13
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
31 let quit = gio::SimpleAction::new("quit", None); |
15
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
32 app.set_accels_for_action("app.quit", &["<Control>q"]); |
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
33 app.add_action(&quit); |
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
34 quit.connect_activate(clone!(@weak app => move |_, _| app.quit())); |
13
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
35 |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
36 let about = gio::SimpleAction::new("about", None); |
15
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
37 app.add_action(&about); |
13
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
38 about.connect_activate(|_, _| { |
15
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
39 let about = gtk::AboutDialog::builder() |
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
40 .program_name("TabletEmu") |
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
41 .logo_icon_name("input-tablet") |
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
42 .website("https://hg.linkmauve.fr/tablet-emu") |
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
43 .version("0.1") |
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
44 .license_type(gtk::License::Agpl30) |
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
45 .copyright("© 2020 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>") |
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
46 .build(); |
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
47 //about.run(); |
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
48 about.destroy(); |
13
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
49 }); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
50 |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
51 let menu = gio::Menu::new(); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
52 { |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
53 let file = gio::Menu::new(); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
54 file.append(Some("_Quit"), Some("app.quit")); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
55 menu.append_submenu(Some("_File"), &file); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
56 } |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
57 { |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
58 let help = gio::Menu::new(); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
59 help.append(Some("_About"), Some("app.about")); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
60 menu.append_submenu(Some("_Help"), &help); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
61 } |
15
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
62 app.set_menubar(Some(&menu)); |
13
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
63 } |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
64 |
15
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
65 fn build_ui(app: >k::Application) { |
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
66 build_main_menu(app); |
13
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
67 |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
68 let state = match State::new() { |
14
adab13145994
Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
13
diff
changeset
|
69 Ok(state) => Arc::new(Mutex::new(state)), |
13
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
70 Err(err) => { |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
71 match err.kind() { |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
72 ErrorKind::NotFound => { |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
73 eprintln!("Couldn’t find /dev/uinput: {}", err); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
74 eprintln!("Maybe you forgot to `modprobe uinput`?"); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
75 } |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
76 ErrorKind::PermissionDenied => { |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
77 eprintln!("Couldn’t open /dev/uinput for writing: {}", err); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
78 eprintln!("Maybe you aren’t allowed to create input devices?"); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
79 } |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
80 _ => eprintln!("Couldn’t open /dev/uinput for writing: {}", err), |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
81 } |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
82 std::process::exit(1); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
83 } |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
84 }; |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
85 |
15
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
86 let hbox = gtk::Box::builder() |
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
87 .orientation(gtk::Orientation::Horizontal) |
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
88 .build(); |
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
89 let tools_box = gtk::Box::builder() |
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
90 .orientation(gtk::Orientation::Vertical) |
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
91 .build(); |
13
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
92 |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
93 macro_rules! impl_tool { |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
94 ($tool:tt) => { |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
95 let tool = gtk::Button::with_mnemonic($tool); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
96 let state_weak = Arc::downgrade(&state); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
97 tool.connect_clicked(move |b| { |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
98 let state = state_weak.upgrade().unwrap(); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
99 let mut state = state.lock().unwrap(); |
15
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
100 let tool = match b.label().unwrap().as_str() { |
13
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
101 "_Pen" => Key::ButtonToolPen, |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
102 "_Rubber" => Key::ButtonToolRubber, |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
103 "_Brush" => Key::ButtonToolBrush, |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
104 "P_encil" => Key::ButtonToolPencil, |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
105 "_Airbrush" => Key::ButtonToolAirbrush, |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
106 _ => unreachable!(), |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
107 }; |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
108 state.select_tool(tool); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
109 }); |
15
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
110 tools_box.append(&tool); |
13
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
111 }; |
15
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
112 } |
13
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
113 impl_tool!("_Pen"); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
114 impl_tool!("_Rubber"); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
115 impl_tool!("_Brush"); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
116 impl_tool!("P_encil"); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
117 impl_tool!("_Airbrush"); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
118 |
15
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
119 let drawing_area = gtk::DrawingArea::builder() |
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
120 .content_width(DEFAULT_WIDTH) |
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
121 .content_height(DEFAULT_HEIGHT) |
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
122 .hexpand(true) |
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
123 .build(); |
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
124 let gesture_click = gtk::GestureClick::builder() |
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
125 .build(); |
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
126 let event_controller = gtk::EventControllerMotion::builder() |
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
127 .build(); |
13
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
128 let state_weak = Arc::downgrade(&state); |
15
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
129 drawing_area.connect_resize(move |_, width, height| { |
13
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
130 let state = state_weak.upgrade().unwrap(); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
131 let mut state = state.lock().unwrap(); |
15
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
132 state.set_size(width, height); |
13
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
133 }); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
134 let state_weak = Arc::downgrade(&state); |
15
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
135 gesture_click.connect_pressed(move |_, n_press, x, y| { |
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
136 if n_press != 1 { |
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
137 return; |
13
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
138 } |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
139 |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
140 let state = state_weak.upgrade().unwrap(); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
141 let mut state = state.lock().unwrap(); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
142 state.press(x, y).unwrap(); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
143 }); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
144 let state_weak = Arc::downgrade(&state); |
15
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
145 gesture_click.connect_released(move |_, n_press, x, y| { |
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
146 if n_press != 1 { |
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
147 return; |
13
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
148 } |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
149 |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
150 let state = state_weak.upgrade().unwrap(); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
151 let mut state = state.lock().unwrap(); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
152 state.release(x, y).unwrap(); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
153 }); |
15
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
154 event_controller.connect_motion(move |_, x, y| { |
13
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
155 let mut state = state.lock().unwrap(); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
156 state.motion(x, y).unwrap(); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
157 }); |
15
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
158 drawing_area.add_controller(&gesture_click); |
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
159 drawing_area.add_controller(&event_controller); |
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
160 drawing_area.set_draw_func(move |_, ctx, _, _| { |
13
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
161 ctx.set_source_rgb(1., 0., 0.); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
162 ctx.set_operator(cairo::Operator::Screen); |
15
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
163 ctx.paint().unwrap(); |
13
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
164 }); |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
165 |
15
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
166 hbox.append(&tools_box); |
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
167 hbox.append(&drawing_area); |
13
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
168 |
15
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
169 let window = gtk::ApplicationWindow::builder() |
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
170 .application(app) |
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
171 .title("tablet-emu") |
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
172 .default_width(800) |
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
173 .default_height(480) |
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
174 .child(&hbox) |
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
175 .build(); |
13
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
176 |
15
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
177 window.show(); |
13
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
178 } |
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
179 |
15
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
180 pub fn main(_args: &[String]) { |
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
181 let app = gtk::Application::builder() |
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
182 .application_id("fr.linkmauve.TabletEmu") |
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
183 .build(); |
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
184 app.connect_activate(build_ui); |
d103f7cca0bd
Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
14
diff
changeset
|
185 app.run(); |
13
97e543f50f62
Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
186 } |