-
-
Notifications
You must be signed in to change notification settings - Fork 177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create ListBox-Sorted-StringList-model #1879
Open
jimbrankelly
wants to merge
11
commits into
gtk-rs:main
Choose a base branch
from
jimbrankelly:patch-1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
629e423
Create ListBox-Sorted-StringList-model
jimbrankelly c3a57df
Update ListBox-Sorted-StringList-model
jimbrankelly 34386f3
Adding README and main.rs
jimbrankelly cecb832
Updated cargo.toml and examples README
jimbrankelly b1d3b85
ran rustfmt
jimbrankelly cc9858f
Update examples/listbox_sort_stringlist/main.rs
jimbrankelly e0b01a5
Update examples/listbox_sort_stringlist/main.rs
jimbrankelly 88602b5
Deleted add and delete buttons
jimbrankelly f9933f1
Revised delete closure
jimbrankelly fd5eecd
Merge branch 'main' into patch-1
jimbrankelly 33bcd85
revised delete button closure
jimbrankelly File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# ListBox and StringListModel with Sorter example | ||
|
||
This example demonstrates how to use `gtk::ListBox` in combination with | ||
a StringList model and StringSorter with a PropertyExpression. | ||
|
||
It sets up a `gtk::ListBox` containing an Inscription on each row. | ||
The rows are sorted alphabetically. | ||
|
||
In addition, it is possible to delete rows. | ||
|
||
Run it by executing: | ||
|
||
```bash | ||
cargo run --bin list_box_sort_stringlist | ||
`` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
// Variation on ListBox model example to show use of StringList and StringSorter. | ||
|
||
use gtk::{ | ||
glib::{self, clone}, | ||
prelude::*, | ||
}; | ||
|
||
fn main() -> glib::ExitCode { | ||
let application = gtk::Application::builder() | ||
.application_id("com.github.gtk-rs.examples.SortedStringList") | ||
.build(); | ||
|
||
application.connect_activate(build_ui); | ||
|
||
application.run() | ||
} | ||
|
||
fn build_ui(application: >k::Application) { | ||
let window = gtk::ApplicationWindow::builder() | ||
.default_width(320) | ||
.default_height(200) | ||
.application(application) | ||
.title("Sorted StringList") | ||
.build(); | ||
|
||
let vbox = gtk::Box::new(gtk::Orientation::Vertical, 5); | ||
|
||
// Create a StringSorter with a property expression to sort | ||
// StringObjects in a StringList. StringObject has a "string" property. | ||
let expression = gtk::PropertyExpression::new( | ||
gtk::StringObject::static_type(), | ||
None::<gtk::Expression>, | ||
"string", | ||
); | ||
let sorter = gtk::StringSorter::new(Some(expression)); | ||
sorter.set_ignore_case(true); | ||
|
||
// Create our list store as a StringList and populate with some strings. | ||
let model = gtk::StringList::new(&["zoo", "abba", "donkey", "sunrise", "river", "phoenix"]); | ||
|
||
// Create a sort model and bind it to the ListStore and the sorter. | ||
let sort_model = gtk::SortListModel::new(Some(model.clone()), Some(sorter)); | ||
|
||
// And then create the UI part, the listbox and bind the sort | ||
// model to it. Whenever the UI needs to show a new row, e.g. because | ||
// it was notified that the model changed, it will call the callback | ||
// with the corresponding item from the model and will ask for a new | ||
// gtk::ListBoxRow that should be displayed. | ||
// | ||
// The gtk::ListBoxRow can contain any possible widgets. | ||
// Here we use an Inscription. | ||
|
||
let listbox = gtk::ListBox::new(); | ||
listbox.bind_model( | ||
Some(&sort_model), | ||
clone!( | ||
#[weak(rename_to = _panel)] | ||
window, | ||
#[upgrade_or_panic] | ||
move |obj| { | ||
let list_object = obj | ||
.downcast_ref::<gtk::StringObject>() | ||
.expect("The object should be of type `StringObject`."); | ||
let row = gtk::Inscription::new(Some(&list_object.string())); | ||
row.set_xalign(0.0); | ||
row.upcast() | ||
} | ||
), | ||
); | ||
|
||
let scrolled_window = gtk::ScrolledWindow::builder() | ||
.hscrollbar_policy(gtk::PolicyType::Never) // Disable horizontal scrolling | ||
.min_content_height(200) | ||
.min_content_width(360) | ||
.build(); | ||
|
||
scrolled_window.set_child(Some(&listbox)); | ||
|
||
let hbox = gtk::Box::new(gtk::Orientation::Horizontal, 5); | ||
// Via the delete button we delete the item from the model that | ||
// is at the index of the selected row. Also deleting from the | ||
// model is immediately reflected in the listbox. | ||
let delete_button = gtk::Button::with_label("Delete"); | ||
delete_button.connect_clicked(clone!( | ||
#[weak] | ||
model, | ||
#[weak] | ||
sort_model, | ||
#[weak] | ||
listbox, | ||
move |_| { | ||
if let Some(selected) = listbox.selected_row() { | ||
// Find the selected object in the sort_model. | ||
let idx = selected.index(); | ||
if let Some(selected_item) = sort_model.item(idx as u32) { | ||
let mut selected_index = None; | ||
// Find the position in the StringList model of the selected_item | ||
for ind in 0..model.n_items() { | ||
if let Some(item) = model.item(ind) { | ||
if item == selected_item { | ||
selected_index = Some(ind); | ||
break; | ||
} | ||
} | ||
} | ||
// remove item from underlying stringlist model | ||
if let Some(index) = selected_index { | ||
model.remove(index); | ||
} | ||
} | ||
} | ||
} | ||
)); | ||
|
||
hbox.append(&delete_button); | ||
|
||
vbox.append(&hbox); | ||
vbox.append(&scrolled_window); | ||
|
||
window.set_child(Some(&vbox)); | ||
|
||
for i in 0..10 { | ||
model.append(&format!("Name {i}")); | ||
} | ||
|
||
window.present(); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like this adds more complexity and it is not really the best way to achieve that. So I would just keep it for implementing a sort with stringlist
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your suggestions. I've removed the "add" and "delete" buttons to simplify the example.