Skip to content

Commit 0c73ece

Browse files
committed
🧹 pages: refactor model find algo
1 parent 3f8c8d0 commit 0c73ece

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

‎src/pages/dns.rs‎

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,11 @@ fn create_connections_section() -> gtk::Box {
6363
// preset the current active connection
6464
if let Some(active_conn_name) = actions::get_active_connection_name() {
6565
let model = combo_conn.model().unwrap();
66-
if let Some(iter) = model.iter_first() {
67-
loop {
68-
if model.value(&iter, 0).get::<String>().unwrap() == active_conn_name {
69-
combo_conn.set_active_iter(Some(&iter));
70-
71-
let selected_dns_index = selection_index_for_connection(&active_conn_name);
72-
combo_servers.set_active(Some(selected_dns_index as u32));
73-
break;
74-
}
75-
if !model.iter_next(&iter) {
76-
break;
77-
}
78-
}
66+
if let Some(iter) = utils::find_iter_in_model(model, active_conn_name) {
67+
combo_conn.set_active_iter(Some(&iter));
68+
69+
let selected_dns_index = selection_index_for_connection(&active_conn_name);
70+
combo_servers.set_active(Some(selected_dns_index as u32));
7971
}
8072
}
8173

‎src/utils.rs‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,27 @@ pub fn check_regular_file(path: &str) -> bool {
7070
}
7171
}
7272

73+
pub fn find_iter_in_model(
74+
model: &impl IsA<gtk::TreeModel>,
75+
search_text: &str,
76+
) -> Option<gtk::TreeIter> {
77+
if let Some(iter) = model.iter_first() {
78+
loop {
79+
if let Ok(value) = model.value(&iter, 0).get::<String>() {
80+
if value == search_text {
81+
return Some(iter);
82+
}
83+
}
84+
85+
if !model.iter_next(&iter) {
86+
break;
87+
}
88+
}
89+
}
90+
91+
None
92+
}
93+
7394
pub fn create_combo_with_model(group_store: &gtk::ListStore) -> gtk::ComboBox {
7495
let group_combo = gtk::ComboBox::with_model(group_store);
7596
let combo_renderer = gtk::CellRendererText::new();

0 commit comments

Comments
 (0)