This repository has been archived by the owner on Mar 4, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 90
/
main.rs
118 lines (98 loc) · 3.84 KB
/
main.rs
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
use gtk::glib;
use gtk::prelude::*;
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
fn main() {
let application = gtk::Application::new(
Some("com.github.gtk-rs.examples.multi_windows"),
Default::default(),
);
application.connect_activate(build_ui);
application.run();
}
fn build_ui(application: >k::Application) {
let windows: Rc<RefCell<HashMap<usize, glib::WeakRef<gtk::Window>>>> =
Rc::new(RefCell::new(HashMap::new()));
let window = create_main_window(application);
// Why not changing all sub-windows' title at once?
let windows_title_entry = gtk::Entry::new();
windows_title_entry.set_placeholder_text(Some("Update all sub-windows' title"));
windows_title_entry.connect_changed(glib::clone!(@weak windows => move |windows_title_entry| {
// When the entry's text is updated, we update the title of every sub windows.
let text = windows_title_entry.buffer().text();
for window in windows.borrow().values() {
if let Some(w) = window.upgrade() {
w.set_title(&text)
}
}
}));
let entry = gtk::Entry::new();
entry.set_editable(false);
entry.set_placeholder_text(Some("Events notification will be sent here"));
// Now let's create a button to create a looooot of new windows!
let button = gtk::Button::with_label("Create new window");
button.connect_clicked(
glib::clone!(@weak windows_title_entry, @weak entry, @weak application => move |_| {
let new_id = generate_new_id(&windows.borrow());
create_sub_window(&application,
&windows_title_entry.buffer().text(),
&entry,
new_id,
&windows);
}),
);
// Now we add a layout so we can put all widgets into it.
let layout = gtk::Box::new(gtk::Orientation::Vertical, 5);
layout.add(&windows_title_entry);
layout.add(&button);
layout.add(&entry);
window.add(&layout);
window.set_focus(Some(&button));
// Then we show everything.
window.show_all();
}
fn create_main_window(application: >k::Application) -> gtk::ApplicationWindow {
let window = gtk::ApplicationWindow::new(application);
window.set_title("I'm the main window");
window.set_default_size(400, 200);
window.set_position(gtk::WindowPosition::Center);
window.show_all();
window
}
fn generate_new_id(windows: &HashMap<usize, glib::WeakRef<gtk::Window>>) -> usize {
let mut id = 0;
// As long as the id is already there, we just continue to increment.
while windows.get(&id).is_some() {
id += 1;
}
id
}
fn create_sub_window(
application: >k::Application,
title: &str,
main_window_entry: >k::Entry,
id: usize,
windows: &Rc<RefCell<HashMap<usize, glib::WeakRef<gtk::Window>>>>,
) {
let window = gtk::Window::new(gtk::WindowType::Toplevel);
application.add_window(&window);
window.set_title(title);
window.set_default_size(400, 200);
window.connect_delete_event(
glib::clone!(@weak windows => @default-return glib::Propagation::Proceed, move |_, _| {
windows.borrow_mut().remove(&id);
glib::Propagation::Proceed
}),
);
let button = gtk::Button::with_label(&format!("Notify main window with id {id}!"));
button.connect_clicked(glib::clone!(@weak main_window_entry => move |_| {
// When the button is clicked, let's write it on the main window's entry!
main_window_entry.buffer().set_text(&format!("sub window {id} clicked"));
}));
window.add(&button);
window.show_all();
// Once the new window has been created, we put it into our hashmap so we can update its
// title when needed.
windows.borrow_mut().insert(id, window.downgrade());
}