@@ -18,7 +18,7 @@ fn main() -> glib::ExitCode {
1818fn build_ui ( application : & gtk:: Application ) {
1919 let window = gtk:: ApplicationWindow :: builder ( )
2020 . default_width ( 320 )
21- . default_height ( 480 )
21+ . default_height ( 200 )
2222 . application ( application)
2323 . title ( "Sorted StringList" )
2424 . build ( ) ;
@@ -70,12 +70,51 @@ fn build_ui(application: >k::Application) {
7070
7171 let scrolled_window = gtk:: ScrolledWindow :: builder ( )
7272 . hscrollbar_policy ( gtk:: PolicyType :: Never ) // Disable horizontal scrolling
73- . min_content_height ( 480 )
73+ . min_content_height ( 200 )
7474 . min_content_width ( 360 )
7575 . build ( ) ;
7676
7777 scrolled_window. set_child ( Some ( & listbox) ) ;
7878
79+ let hbox = gtk:: Box :: new ( gtk:: Orientation :: Horizontal , 5 ) ;
80+ // Via the delete button we delete the item from the model that
81+ // is at the index of the selected row. Also deleting from the
82+ // model is immediately reflected in the listbox.
83+ let delete_button = gtk:: Button :: with_label ( "Delete" ) ;
84+ delete_button. connect_clicked ( clone ! (
85+ #[ weak]
86+ model,
87+ #[ weak]
88+ sort_model,
89+ #[ weak]
90+ listbox,
91+ move |_| {
92+ if let Some ( selected) = listbox. selected_row( ) {
93+ // Find the selected object in the sort_model.
94+ let idx = selected. index( ) ;
95+ if let Some ( selected_item) = sort_model. item( idx as u32 ) {
96+ let mut selected_index = None ;
97+ // Find the position in the StringList model of the selected_item
98+ for ind in 0 ..model. n_items( ) {
99+ if let Some ( item) = model. item( ind) {
100+ if item == selected_item {
101+ selected_index = Some ( ind) ;
102+ break ;
103+ }
104+ }
105+ }
106+ // remove item from underlying stringlist model
107+ if let Some ( index) = selected_index {
108+ model. remove( index) ;
109+ }
110+ }
111+ }
112+ }
113+ ) ) ;
114+
115+ hbox. append ( & delete_button) ;
116+
117+ vbox. append ( & hbox) ;
79118 vbox. append ( & scrolled_window) ;
80119
81120 window. set_child ( Some ( & vbox) ) ;
0 commit comments