From bfa7d7b21312147a5f4957878704daf4dadd0bcd Mon Sep 17 00:00:00 2001 From: Olaf Wintermann Date: Thu, 7 May 2026 22:16:52 +0200 Subject: [PATCH] implement ListSelection event data type --- ui-rs/src/ui/event.rs | 11 +++++---- ui-rs/src/ui/toolkit.rs | 51 ++++++++++++++++++++++++++++++----------- 2 files changed, 44 insertions(+), 18 deletions(-) diff --git a/ui-rs/src/ui/event.rs b/ui-rs/src/ui/event.rs index 0a1ff3c..80659f8 100644 --- a/ui-rs/src/ui/event.rs +++ b/ui-rs/src/ui/event.rs @@ -29,7 +29,7 @@ #![allow(dead_code)] use std::ffi::{c_char, c_void, CStr, CString}; -use crate::ui::{event, ffi, UiDouble, UiInteger, UiString, UiText, UiRange}; +use crate::ui::{event, ffi, UiDouble, UiInteger, UiString, UiText, UiRange, ListSelection}; use crate::ui::ffi::{UiEvent}; pub struct Event<'a, T> { @@ -54,7 +54,7 @@ pub enum EventType<'a> { TextValue(&'a mut UiText), DoubleValue(&'a mut UiDouble), RangeValue(&'a mut UiRange), - ListSelection, + ListSelection(&'a ListSelection), ListElement, Dnd, SubList, @@ -70,7 +70,7 @@ enum EventTypeData { TextValue(UiText), DoubleValue(UiDouble), RangeValue(UiRange), - ListSelection, + ListSelection(ListSelection), ListElement, Dnd, SubList, @@ -101,6 +101,9 @@ fn get_event_data(e: *const ffi::UiEvent) -> EventTypeData { let d: *mut ffi::UiRange = ptr.cast(); EventTypeData::RangeValue( UiRange { ptr: d } ) } + 8 => { + EventTypeData::ListSelection( ListSelection::from_ptr(ptr.cast()) ) + } _ => EventTypeData::Null } } @@ -115,7 +118,7 @@ fn get_event_type<'a>(data: &'a mut EventTypeData) -> EventType<'a> { EventTypeData::TextValue(t) => EventType::TextValue( t ), EventTypeData::DoubleValue(d) => EventType::DoubleValue( d ), EventTypeData::RangeValue(r) => EventType::RangeValue( r ), - EventTypeData::ListSelection => EventType::ListSelection, + EventTypeData::ListSelection(s) => EventType::ListSelection( s), EventTypeData::ListElement => EventType::ListElement, EventTypeData::Dnd => EventType::Dnd, EventTypeData::SubList => EventType::SubList, diff --git a/ui-rs/src/ui/toolkit.rs b/ui-rs/src/ui/toolkit.rs index d80c76f..3330b39 100644 --- a/ui-rs/src/ui/toolkit.rs +++ b/ui-rs/src/ui/toolkit.rs @@ -454,20 +454,13 @@ impl UiList { } pub fn selection(&mut self) -> Vec { - let mut sel = Vec::new(); unsafe { - let selection = ui_list_get_selection_allocated(self.ptr); - let count = ui_list_selection_get_count(selection) as usize; - if count > 0 { - let indices = ui_list_selection_get_rows(selection); - sel.set_len(count as usize); - for i in 0..count { - sel.push(*indices.add(i) as i32); - } - } - ui_list_selection_free(selection); + let selection = ListSelection { + ptr: ui_list_get_selection_allocated(self.ptr), + free: true + }; + selection.selection() } - sel } pub fn set_selection(&mut self, sel: &Vec) { @@ -499,6 +492,36 @@ impl Drop for UiList { } } +pub struct ListSelection { + pub ptr: *mut ffi::UiListSelection, + free: bool +} + +impl ListSelection { + pub fn from_ptr(ptr: *mut ffi::UiListSelection) -> ListSelection { + ListSelection { ptr: ptr, free: false } + } + + pub fn from_allocated(ptr: *mut ffi::UiListSelection) -> ListSelection { + ListSelection { ptr: ptr, free: true } + } + + pub fn selection(&self) -> Vec { + let mut sel = Vec::new(); + unsafe { + let count = ui_list_selection_get_count(self.ptr) as usize; + if count > 0 { + let indices = ui_list_selection_get_rows(self.ptr); + sel.set_len(count as usize); + for i in 0..count { + sel.push(*indices.add(i) as i32); + } + } + } + sel + } +} + /* -------------------------------- SourceList -------------------------------- */ @@ -739,8 +762,8 @@ extern "C" { fn ui_list_update_row(list: *mut ffi::UiList, row: c_int); fn ui_list_get_selection_allocated(list: *mut ffi::UiList) -> *mut ffi::UiListSelection; fn ui_list_set_selected_indices(list: *mut ffi::UiList, indices: *const c_int, len: c_int); - fn ui_list_selection_get_count(list: *mut ffi::UiListSelection) -> c_int; - fn ui_list_selection_get_rows(list: *mut ffi::UiListSelection) -> *const c_int; + fn ui_list_selection_get_count(list: *const ffi::UiListSelection) -> c_int; + fn ui_list_selection_get_rows(list: *const ffi::UiListSelection) -> *const c_int; fn ui_list_selection_free(list: *mut ffi::UiListSelection); pub fn ui_list_append(list: *mut ffi::UiList, data: *mut c_void); -- 2.47.3