Skip to content

Commit 7a10933

Browse files
authored
Merge pull request #148 from kas-gui/work2
Filter list
2 parents de67f57 + d26dec8 commit 7a10933

File tree

15 files changed

+822
-303
lines changed

15 files changed

+822
-303
lines changed

.travis.yml

Lines changed: 0 additions & 22 deletions
This file was deleted.

kas-theme/src/dim.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ impl<'a> draw::SizeHandle for SizeHandle<'a> {
229229
}
230230

231231
fn edit_surround(&self) -> (Size, Size) {
232-
let s = Size::uniform(self.dims.frame as u32);
232+
let s = Size::uniform(self.dims.frame as u32 + self.dims.inner_margin as u32);
233233
(s, s)
234234
}
235235

kas-wgpu/examples/dynamic.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ struct ListEntryGuard(usize);
4545
impl EditGuard for ListEntryGuard {
4646
type Msg = EntryMsg;
4747

48-
fn edit(entry: &mut EditBox<Self>) -> Option<Self::Msg> {
48+
fn edit(entry: &mut EditBox<Self>, _: &mut Manager) -> Option<Self::Msg> {
4949
Some(EntryMsg::Update(entry.guard.0, entry.get_string()))
5050
}
5151
}
@@ -99,7 +99,7 @@ fn main() -> Result<(), kas_wgpu::Error> {
9999
struct {
100100
#[widget] _ = Label::new("Number of rows:"),
101101
#[widget(handler = activate)] edit: impl HasString = EditBox::new("3")
102-
.on_afl(|text| text.parse::<usize>().ok()),
102+
.on_afl(|text, _| text.parse::<usize>().ok()),
103103
#[widget(handler = button)] _ = TextButton::new("Set", Control::Set),
104104
#[widget(handler = button)] _ = TextButton::new("−", Control::Decr),
105105
#[widget(handler = button)] _ = TextButton::new("+", Control::Incr),
@@ -148,7 +148,7 @@ fn main() -> Result<(), kas_wgpu::Error> {
148148
impl {
149149
fn set_len(&mut self, mgr: &mut Manager, len: usize) -> Response<VoidMsg> {
150150
let active = self.active;
151-
let old_len = self.list.inner().len();
151+
let old_len = self.list.len();
152152
*mgr += self.list.inner_mut().resize_with(len, |n| ListEntry::new(n, n == active));
153153
if active >= old_len && active < len {
154154
let _ = self.set_radio(mgr, EntryMsg::Select(active));
@@ -159,7 +159,7 @@ fn main() -> Result<(), kas_wgpu::Error> {
159159
match msg {
160160
EntryMsg::Select(n) => {
161161
self.active = n;
162-
let text = self.list.inner()[n].entry.get_string();
162+
let text = self.list[n].entry.get_string();
163163
*mgr += self.display.set_string(text);
164164
}
165165
EntryMsg::Update(n, text) => {

kas-wgpu/examples/filter-list.rs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55

66
//! Filter list example
77
8-
use kas::widget::view::{ListView, SharedConst};
9-
use kas::widget::Window;
8+
use kas::prelude::*;
9+
use kas::widget::view::{FilterAccessor, ListView, SharedConst};
10+
use kas::widget::{EditBox, Window};
11+
use std::cell::RefCell;
12+
use std::rc::Rc;
1013

1114
const MONTHS: &[&str] = &[
1215
"January",
@@ -26,8 +29,31 @@ const MONTHS: &[&str] = &[
2629
fn main() -> Result<(), kas_wgpu::Error> {
2730
env_logger::init();
2831

29-
let data: &SharedConst<[&str]> = MONTHS.into();
30-
let window = Window::new("Filter-list", ListView::<kas::Down, _>::new(data));
32+
type SC = &'static SharedConst<[&'static str]>;
33+
type FA = Rc<RefCell<FilterAccessor<usize, SC>>>;
34+
let data: SC = MONTHS.into();
35+
let data = Rc::new(RefCell::new(FilterAccessor::new_visible(data)));
36+
let data2 = data.clone();
37+
let window = Window::new(
38+
"Filter-list",
39+
make_widget! {
40+
#[layout(down)]
41+
#[handler(msg = VoidMsg)]
42+
struct {
43+
#[widget] filter = EditBox::new("").on_edit(move |text, mgr| {
44+
// Note: this method of caseless matching is not unicode compliant!
45+
// https://stackoverflow.com/questions/47298336/case-insensitive-string-matching-in-rust
46+
let text = text.to_uppercase();
47+
let update = data2
48+
.borrow_mut()
49+
.update_filter(|s| s.to_uppercase().contains(&text));
50+
mgr.trigger_update(update, 0);
51+
None
52+
}),
53+
#[widget] list = ListView::<kas::Down, FA>::new(data),
54+
}
55+
},
56+
);
3157

3258
let theme = kas_theme::ShadedTheme::new();
3359
kas_wgpu::Toolkit::new(theme)?.with(window)?.run()

kas-wgpu/examples/gallery.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ struct Guard;
2828
impl EditGuard for Guard {
2929
type Msg = Item;
3030

31-
fn activate(edit: &mut EditBox<Self>) -> Option<Self::Msg> {
31+
fn activate(edit: &mut EditBox<Self>, _: &mut Manager) -> Option<Self::Msg> {
3232
Some(Item::Edit(edit.get_string()))
3333
}
3434

35-
fn edit(edit: &mut EditBox<Self>) -> Option<Self::Msg> {
35+
fn edit(edit: &mut EditBox<Self>, _: &mut Manager) -> Option<Self::Msg> {
3636
// 7a is the colour of *magic*!
3737
edit.set_error_state(edit.get_str().len() % (7 + 1) == 0);
3838
None
@@ -233,7 +233,7 @@ fn main() -> Result<(), kas_wgpu::Error> {
233233
mgr.adjust_theme(|theme| theme.set_colours(name));
234234
}
235235
Menu::Disabled(state) => {
236-
*mgr += self.gallery.inner_mut().set_disabled(state);
236+
*mgr += self.gallery.set_disabled(state);
237237
}
238238
Menu::Quit => {
239239
*mgr += TkAction::CloseAll;

kas-wgpu/examples/markdown.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ It also supports lists:
5757
}
5858
};
5959
// TODO: this should update the size requirements of the inner area
60-
*mgr += self.label.inner_mut().set_text(text);
60+
*mgr += self.label.set_text(text);
6161
Response::None
6262
}
6363
}

src/data.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use std::rc::Rc;
1313
use std::u32;
1414

1515
use super::Align;
16-
use crate::geom::{Rect, Size};
16+
use crate::geom::{Coord, Rect, Size};
1717

1818
// for doc use
1919
#[allow(unused)]
@@ -263,6 +263,26 @@ pub trait Directional: Copy + Sized + std::fmt::Debug + 'static {
263263
fn is_reversed(self) -> bool {
264264
((self.as_direction() as u32) & 2) == 2
265265
}
266+
267+
/// Extract a coordinate
268+
#[inline]
269+
fn extract_coord(self, coord: Coord) -> i32 {
270+
if self.is_horizontal() {
271+
coord.0
272+
} else {
273+
coord.1
274+
}
275+
}
276+
277+
/// Extract a size
278+
#[inline]
279+
fn extract_size(self, size: Size) -> u32 {
280+
if self.is_horizontal() {
281+
size.0
282+
} else {
283+
size.1
284+
}
285+
}
266286
}
267287

268288
macro_rules! fixed {

src/draw/handle.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,13 @@ pub trait SizeHandle {
139139

140140
/// The margin around content within a widget
141141
///
142-
/// This area may be used to draw focus indicators.
142+
/// Though inner margins are *usually* empty, they are sometimes drawn to,
143+
/// for example focus indicators.
143144
fn inner_margin(&self) -> Size;
144145

145146
/// The margin between UI elements, where desired
147+
///
148+
/// Widgets must not draw in outer margins.
146149
fn outer_margins(&self) -> Margins;
147150

148151
/// The height of a line of text
@@ -173,10 +176,13 @@ pub trait SizeHandle {
173176
/// Excludes size of content area.
174177
fn button_surround(&self) -> (Size, Size);
175178

176-
/// Size of the sides of an edit box.
179+
/// Size of the frame around an edit box, including margin
177180
///
178-
/// Returns `(top_left, bottom_right)` dimensions as two `Size`s.
179-
/// Excludes size of content area.
181+
/// Returns two [`Size`] values, `(tl, br)`. `tl` is the size of left and
182+
/// top sides, and `br` is the size of right and bottom sides.
183+
///
184+
/// Note: though text should not be drawn in the margin, the edit cursor
185+
/// may be. The margin included here should be large enough!
180186
fn edit_surround(&self) -> (Size, Size);
181187

182188
/// Size of the element drawn by [`DrawHandle::checkbox`].

src/geom.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,24 @@ impl std::ops::Add<Size> for Coord {
101101
}
102102
}
103103

104+
impl std::ops::Sub<Size> for Coord {
105+
type Output = Self;
106+
107+
#[inline]
108+
fn sub(self, other: Size) -> Self {
109+
Coord(self.0 - other.0 as i32, self.1 - other.1 as i32)
110+
}
111+
}
112+
113+
impl std::ops::Mul<i32> for Coord {
114+
type Output = Self;
115+
116+
#[inline]
117+
fn mul(self, rhs: i32) -> Self {
118+
Coord(self.0 * rhs, self.1 * rhs)
119+
}
120+
}
121+
104122
impl From<Coord> for kas_text::Vec2 {
105123
fn from(pos: Coord) -> kas_text::Vec2 {
106124
kas_text::Vec2(pos.0 as f32, pos.1 as f32)

0 commit comments

Comments
 (0)