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
executable file
·96 lines (82 loc) · 2.9 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
use gtk::prelude::*;
use gtk::{gio, glib};
fn main() {
let application = gtk::Application::new(
Some("com.github.gtk-rs.examples.entry-completion"),
Default::default(),
);
application.connect_activate(build_ui);
// When activated, shuts down the application
let quit = gio::SimpleAction::new("quit", None);
quit.connect_activate(
glib::clone!(@weak application => move |_action, _parameter| {
application.quit();
}),
);
application.connect_startup(|application| {
application.set_accels_for_action("app.quit", &["<Primary>Q"]);
});
application.add_action(&quit);
// Run the application
application.run();
}
fn build_ui(application: >k::Application) {
// create the main window
let window = gtk::ApplicationWindow::new(application);
window.set_title("Entry with autocompletion");
window.set_border_width(5);
window.set_position(gtk::WindowPosition::Center);
window.set_default_size(840, 480);
// Create a title label
let win_title = gtk::Label::new(None);
win_title.set_markup("<big>Which country would you like to spend a holiday in?</big>");
// Create an EntryCompletion widget
let completion_countries = gtk::EntryCompletion::new();
// Use the first (and only) column available to set the autocompletion text
completion_countries.set_text_column(0);
// how many keystrokes to wait before attempting to autocomplete?
completion_countries.set_minimum_key_length(1);
// whether the completions should be presented in a popup window
completion_countries.set_popup_completion(true);
// Create a ListStore of items
// These will be the source for the autocompletion
// as the user types into the field
// For a more evolved example of ListStore see src/bin/list_store.rs
let ls = create_list_model();
completion_countries.set_model(Some(&ls));
let input_field = gtk::Entry::new();
input_field.set_completion(Some(&completion_countries));
let row = gtk::Box::new(gtk::Orientation::Vertical, 5);
row.add(&win_title);
row.pack_start(&input_field, false, false, 10);
// window.add(&win_title);
window.add(&row);
// show everything
window.show_all();
}
struct Data {
description: String,
}
fn create_list_model() -> gtk::ListStore {
let col_types: [glib::Type; 1] = [glib::Type::STRING];
let data: [Data; 4] = [
Data {
description: "France".to_string(),
},
Data {
description: "Italy".to_string(),
},
Data {
description: "Sweden".to_string(),
},
Data {
description: "Switzerland".to_string(),
},
];
let store = gtk::ListStore::new(&col_types);
for d in data.iter() {
let values: [(u32, &dyn ToValue); 1] = [(0, &d.description)];
store.set(&store.append(), &values);
}
store
}