annotate src/bin/standalone.rs @ 18:3f7b7a3ad8fe

Build three binaries instead of using arguments.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Thu, 26 Aug 2021 22:15:55 +0200
parents src/gtk.rs@478cf2a7d577
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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::*;
16
478cf2a7d577 Reformat with rustfmt.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 15
diff changeset
20 use input_linux::Key;
13
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};
18
3f7b7a3ad8fe Build three binaries instead of using arguments.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 16
diff changeset
23 use tablet_emu::{state::State, DEFAULT_HEIGHT, DEFAULT_WIDTH};
13
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
24
15
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
25 fn build_main_menu(app: &gtk::Application) {
13
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
26 let quit = gio::SimpleAction::new("quit", None);
15
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
27 app.set_accels_for_action("app.quit", &["<Control>q"]);
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
28 app.add_action(&quit);
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
29 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
30
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
31 let about = gio::SimpleAction::new("about", None);
15
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
32 app.add_action(&about);
13
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
33 about.connect_activate(|_, _| {
15
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
34 let about = gtk::AboutDialog::builder()
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
35 .program_name("TabletEmu")
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
36 .logo_icon_name("input-tablet")
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
37 .website("https://hg.linkmauve.fr/tablet-emu")
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
38 .version("0.1")
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
39 .license_type(gtk::License::Agpl30)
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
40 .copyright("© 2020 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>")
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
41 .build();
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
42 //about.run();
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
43 about.destroy();
13
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
44 });
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
45
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
46 let menu = gio::Menu::new();
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
47 {
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
48 let file = gio::Menu::new();
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
49 file.append(Some("_Quit"), Some("app.quit"));
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
50 menu.append_submenu(Some("_File"), &file);
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
51 }
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 help = gio::Menu::new();
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
54 help.append(Some("_About"), Some("app.about"));
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
55 menu.append_submenu(Some("_Help"), &help);
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
56 }
15
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
57 app.set_menubar(Some(&menu));
13
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
58 }
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
59
15
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
60 fn build_ui(app: &gtk::Application) {
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
61 build_main_menu(app);
13
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
62
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
63 let state = match State::new() {
14
adab13145994 Add support for remote clients.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 13
diff changeset
64 Ok(state) => Arc::new(Mutex::new(state)),
13
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
65 Err(err) => {
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
66 match err.kind() {
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
67 ErrorKind::NotFound => {
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
68 eprintln!("Couldn’t find /dev/uinput: {}", err);
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
69 eprintln!("Maybe you forgot to `modprobe uinput`?");
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
70 }
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
71 ErrorKind::PermissionDenied => {
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
72 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
73 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
74 }
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
75 _ => 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
76 }
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
77 std::process::exit(1);
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
78 }
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
15
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
81 let hbox = gtk::Box::builder()
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
82 .orientation(gtk::Orientation::Horizontal)
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
83 .build();
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
84 let tools_box = gtk::Box::builder()
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
85 .orientation(gtk::Orientation::Vertical)
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
86 .build();
13
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
87
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
88 macro_rules! impl_tool {
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
89 ($tool:tt) => {
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
90 let tool = gtk::Button::with_mnemonic($tool);
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
91 let state_weak = Arc::downgrade(&state);
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
92 tool.connect_clicked(move |b| {
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
93 let state = state_weak.upgrade().unwrap();
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
94 let mut state = state.lock().unwrap();
15
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
95 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
96 "_Pen" => Key::ButtonToolPen,
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
97 "_Rubber" => Key::ButtonToolRubber,
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
98 "_Brush" => Key::ButtonToolBrush,
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
99 "P_encil" => Key::ButtonToolPencil,
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
100 "_Airbrush" => Key::ButtonToolAirbrush,
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
101 _ => unreachable!(),
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
102 };
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
103 state.select_tool(tool);
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
104 });
15
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
105 tools_box.append(&tool);
13
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
106 };
15
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
107 }
13
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
108 impl_tool!("_Pen");
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
109 impl_tool!("_Rubber");
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
110 impl_tool!("_Brush");
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
111 impl_tool!("P_encil");
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
112 impl_tool!("_Airbrush");
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
113
15
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
114 let drawing_area = gtk::DrawingArea::builder()
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
115 .content_width(DEFAULT_WIDTH)
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
116 .content_height(DEFAULT_HEIGHT)
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
117 .hexpand(true)
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
118 .build();
16
478cf2a7d577 Reformat with rustfmt.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 15
diff changeset
119 let gesture_click = gtk::GestureClick::new();
478cf2a7d577 Reformat with rustfmt.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 15
diff changeset
120 let event_controller = gtk::EventControllerMotion::new();
13
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
121 let state_weak = Arc::downgrade(&state);
15
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
122 drawing_area.connect_resize(move |_, width, height| {
13
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
123 let state = state_weak.upgrade().unwrap();
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
124 let mut state = state.lock().unwrap();
15
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
125 state.set_size(width, height);
13
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
126 });
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
127 let state_weak = Arc::downgrade(&state);
15
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
128 gesture_click.connect_pressed(move |_, n_press, x, y| {
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
129 if n_press != 1 {
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
130 return;
13
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
131 }
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
132
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
133 let state = state_weak.upgrade().unwrap();
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
134 let mut state = state.lock().unwrap();
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
135 state.press(x, y).unwrap();
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
136 });
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
137 let state_weak = Arc::downgrade(&state);
15
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
138 gesture_click.connect_released(move |_, n_press, x, y| {
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
139 if n_press != 1 {
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
140 return;
13
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
141 }
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
142
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
143 let state = state_weak.upgrade().unwrap();
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
144 let mut state = state.lock().unwrap();
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
145 state.release(x, y).unwrap();
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
146 });
15
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
147 event_controller.connect_motion(move |_, x, y| {
13
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
148 let mut state = state.lock().unwrap();
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
149 state.motion(x, y).unwrap();
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
150 });
15
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
151 drawing_area.add_controller(&gesture_click);
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
152 drawing_area.add_controller(&event_controller);
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
153 drawing_area.set_draw_func(move |_, ctx, _, _| {
13
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
154 ctx.set_source_rgb(1., 0., 0.);
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
155 ctx.set_operator(cairo::Operator::Screen);
15
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
156 ctx.paint().unwrap();
13
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
157 });
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
158
15
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
159 hbox.append(&tools_box);
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
160 hbox.append(&drawing_area);
13
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
161
15
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
162 let window = gtk::ApplicationWindow::builder()
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
163 .application(app)
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
164 .title("tablet-emu")
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
165 .default_width(800)
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
166 .default_height(480)
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
167 .child(&hbox)
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
168 .build();
13
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
169
15
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
170 window.show();
13
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
171 }
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
172
18
3f7b7a3ad8fe Build three binaries instead of using arguments.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 16
diff changeset
173 pub fn main() {
15
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
174 let app = gtk::Application::builder()
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
175 .application_id("fr.linkmauve.TabletEmu")
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
176 .build();
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
177 app.connect_activate(build_ui);
d103f7cca0bd Update to GTK 4.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 14
diff changeset
178 app.run();
13
97e543f50f62 Split GTK UI into another module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
179 }