use super::*;
use crate::core;
use std::cell::RefCell;
use std::collections::{HashMap, VecDeque};
use std::sync::atomic::{AtomicBool, Ordering};
#[derive(Clone, Debug, Default)]
pub struct MockWidget {
pub kind: Option<Kind>,
pub parent: Option<WidgetId>,
pub text: String,
pub tooltip: String,
pub placeholder: String,
pub accel: String,
pub enabled: bool,
pub visible: bool,
pub checked: bool,
pub readonly: bool,
pub indeterminate: bool,
pub focused: bool,
pub value: f64,
pub range: (f64, f64, f64),
pub items: Vec<String>,
pub selected: Option<usize>,
pub bounds: Rect,
pub image: Option<ImageData>,
pub bounds_pushes: u32,
pub columns: Vec<Column>,
pub rows: Vec<Vec<String>>,
pub sort: Option<(usize, bool)>,
pub tree_rows: Vec<TreeRow>,
pub tree_selected: Option<u64>,
pub orientation: Option<Orientation>,
pub monospace: bool,
pub wrap: bool,
pub position: Option<(i32, i32)>,
pub min_size: Size,
}
#[derive(Clone, Debug, PartialEq)]
pub struct PopupRecord {
pub menu: WidgetId,
pub window: Option<WidgetId>,
pub at: Option<(i32, i32)>,
pub items: Vec<(Kind, String, bool)>,
}
#[derive(Default)]
struct State {
widgets: HashMap<WidgetId, MockWidget>,
order: Vec<WidgetId>,
timers: HashMap<u64, (u32, bool, u32)>, answers: VecDeque<Answer>,
files: VecDeque<Vec<String>>,
last_message: Option<MessageSpec>,
last_file_spec: Option<FileSpec>,
fail_create: bool,
unsupported: Vec<Kind>,
popups: Vec<PopupRecord>,
popup_choices: VecDeque<Option<WidgetId>>,
}
thread_local! { static S: RefCell<State> = RefCell::new(State::default()); }
static WOKEN: AtomicBool = AtomicBool::new(false);
fn st<R>(f: impl FnOnce(&mut State) -> R) -> R {
S.with(|s| f(&mut s.borrow_mut()))
}
pub struct Mock;
impl Backend for Mock {
fn init(_app_name: &str) -> Result<()> {
st(|s| *s = State::default());
Ok(())
}
fn run() {
pump();
}
fn quit() {}
fn wake() {
WOKEN.store(true, Ordering::SeqCst);
}
fn timer_start(token: u64, millis: u32, repeat: bool) -> Result<()> {
st(|s| s.timers.insert(token, (millis, repeat, 0)));
Ok(())
}
fn timer_stop(token: u64) {
st(|s| s.timers.remove(&token));
}
fn create(id: WidgetId, kind: Kind, parent: Option<WidgetId>) -> Result<()> {
st(|s| {
if s.fail_create {
s.fail_create = false;
return Err(Error::Backend("mock: create failed".into()));
}
if s.unsupported.contains(&kind) {
return Err(Error::Unsupported);
}
if let Some(p) = parent {
if !s.widgets.contains_key(&p) {
return Err(Error::Backend("mock: unknown parent".into()));
}
}
s.widgets.insert(
id,
MockWidget {
kind: Some(kind),
parent,
enabled: true,
visible: kind != Kind::Window,
range: (0.0, 100.0, 1.0),
wrap: true,
..Default::default()
},
);
s.order.push(id);
Ok(())
})
}
fn destroy(id: WidgetId) {
st(|s| {
s.widgets.remove(&id);
s.order.retain(|i| *i != id);
});
}
fn set(id: WidgetId, prop: &Prop) {
st(|s| {
let Some(w) = s.widgets.get_mut(&id) else {
return;
};
match prop {
Prop::Text(t) => w.text = t.to_string(),
Prop::Tooltip(t) => w.tooltip = t.to_string(),
Prop::Placeholder(t) => w.placeholder = t.to_string(),
Prop::Enabled(b) => w.enabled = *b,
Prop::Visible(b) => w.visible = *b,
Prop::Checked(b) => w.checked = *b,
Prop::Value(v) => w.value = *v,
Prop::Range { min, max, step } => w.range = (*min, *max, *step),
Prop::Items(i) => w.items = i.to_vec(),
Prop::Selected(i) => w.selected = *i,
Prop::Bounds(r) => {
w.bounds = *r;
w.bounds_pushes += 1;
}
Prop::Image(i) => w.image = i.cloned(),
Prop::Accel(a) => w.accel = a.to_string(),
Prop::ReadOnly(b) => w.readonly = *b,
Prop::Indeterminate(b) => w.indeterminate = *b,
Prop::Focus => w.focused = true,
Prop::Columns(c) => w.columns = c.to_vec(),
Prop::Rows(r) => w.rows = r.to_vec(),
Prop::SortIndicator(x) => w.sort = *x,
Prop::TreeRows(r) => w.tree_rows = r.to_vec(),
Prop::TreeSelected(x) => w.tree_selected = *x,
Prop::Orientation(o) => w.orientation = Some(*o),
Prop::Monospace(b) => w.monospace = *b,
Prop::Wrap(b) => w.wrap = *b,
Prop::Position { x, y } => w.position = Some((*x, *y)),
Prop::MinSize(m) => w.min_size = *m,
_ => {}
}
});
}
fn preferred_size(id: WidgetId) -> Size {
st(|s| {
let Some(w) = s.widgets.get(&id) else {
return Size::default();
};
let tw = 8 * w.text.chars().count() as i32;
match w.kind {
Some(Kind::Label) => Size::new(tw, 16),
Some(Kind::Button) => Size::new(tw + 24, 28),
Some(Kind::CheckBox | Kind::RadioButton) => Size::new(tw + 24, 20),
Some(Kind::TextInput | Kind::PasswordInput) => Size::new(160, 24),
Some(Kind::TextArea) => Size::new(if w.monospace { 240 } else { 200 }, 100),
Some(Kind::ComboBox) => Size::new(140, 26),
Some(Kind::ListBox) => Size::new(160, 100),
Some(Kind::Table) => Size::new(300, 150),
Some(Kind::Tree) => Size::new(200, 200),
Some(Kind::Slider) => Size::new(150, 24),
Some(Kind::ProgressBar) => Size::new(150, 16),
Some(Kind::SpinBox) => Size::new(80, 24),
Some(Kind::Image) => w
.image
.as_ref()
.map_or(Size::new(32, 32), |i| Size::new(i.w as i32, i.h as i32)),
_ => Size::default(),
}
})
}
fn chrome(id: WidgetId) -> Size {
st(|s| match s.widgets.get(&id).and_then(|w| w.kind) {
Some(Kind::GroupBox) => Size::new(12, 24),
Some(Kind::Tabs) => Size::new(4, 28),
_ => Size::default(),
})
}
fn native_handle(id: WidgetId) -> Option<NativeHandle> {
st(|s| s.widgets.contains_key(&id)).then_some(NativeHandle::Gtk(id.0 as usize))
}
fn message_box(_parent: Option<WidgetId>, spec: &MessageSpec) -> Answer {
st(|s| {
s.last_message = Some(spec.clone());
s.answers.pop_front().unwrap_or(Answer::Ok)
})
}
fn file_dialog(_parent: Option<WidgetId>, spec: &FileSpec) -> Vec<String> {
st(|s| {
s.last_file_spec = Some(spec.clone());
s.files.pop_front().unwrap_or_default()
})
}
fn popup_menu(menu: WidgetId, parent_window: Option<WidgetId>, at: Option<(i32, i32)>) {
let choice = st(|s| {
let items = s
.order
.iter()
.filter_map(|c| s.widgets.get(c))
.filter(|w| w.parent == Some(menu))
.map(|w| (w.kind.unwrap_or(Kind::Spacer), w.text.clone(), w.enabled))
.collect();
s.popups.push(PopupRecord {
menu,
window: parent_window,
at,
items,
});
s.popup_choices.pop_front().flatten()
});
let Some(item) = choice else { return };
let (kind, checked, ok) = st(|s| {
let mut ok = false;
let mut cur = Some(item);
while let Some(c) = cur {
let Some(w) = s.widgets.get(&c) else { break };
if !w.enabled {
break;
}
if w.parent == Some(menu) {
ok = true;
break;
}
cur = w.parent;
}
let w = s.widgets.get(&item);
(w.and_then(|w| w.kind), w.is_some_and(|w| w.checked), ok)
});
if !ok {
return;
}
match kind {
Some(Kind::MenuItem) => user(item, Event::Click),
Some(Kind::CheckMenuItem) => user(item, Event::Toggled(!checked)),
_ => {}
}
}
}
pub fn widget(id: WidgetId) -> Option<MockWidget> {
st(|s| s.widgets.get(&id).cloned())
}
pub fn widget_count() -> usize {
st(|s| s.widgets.len())
}
pub fn pump() {
WOKEN.store(false, Ordering::SeqCst);
core::drain_posted(); }
pub fn advance(ms: u32) {
let due: Vec<u64> = st(|s| {
let mut due = vec![];
for (tok, (period, _, el)) in s.timers.iter_mut() {
*el += ms;
while *el >= *period {
*el -= *period;
due.push(*tok);
if *period == 0 {
break;
}
}
}
due
});
for tok in due {
core::timer_fired(tok);
}
}
pub fn timer_count() -> usize {
st(|s| s.timers.len())
}
pub fn user(id: WidgetId, ev: Event) {
st(|s| {
if let Some(w) = s.widgets.get_mut(&id) {
match &ev {
Event::Text(t) => w.text = t.clone(),
Event::Toggled(b) => w.checked = *b,
Event::Selected(i) => w.selected = *i,
Event::Value(v) => w.value = *v,
_ => {}
}
}
});
core::event(id, ev);
}
pub fn user_click(id: WidgetId) {
user(id, Event::Click)
}
pub fn user_text(id: WidgetId, t: &str) {
user(id, Event::Text(t.into()))
}
pub fn resize_window(id: WidgetId, w: i32, h: i32) {
st(|s| {
if let Some(x) = s.widgets.get_mut(&id) {
x.bounds = Rect::new(0, 0, w, h);
}
});
core::event(id, Event::Resized { w, h });
}
pub fn user_close(id: WidgetId) {
core::close_requested(id)
}
pub fn queue_answer(a: Answer) {
st(|s| s.answers.push_back(a));
}
pub fn queue_files(f: &[&str]) {
st(|s| s.files.push_back(f.iter().map(|x| x.to_string()).collect()));
}
pub fn last_message() -> Option<MessageSpec> {
st(|s| s.last_message.clone())
}
pub fn last_file_spec() -> Option<FileSpec> {
st(|s| s.last_file_spec.clone())
}
pub fn queue_popup_choice(item: Option<WidgetId>) {
st(|s| s.popup_choices.push_back(item));
}
pub fn popup_log() -> Vec<PopupRecord> {
st(|s| s.popups.clone())
}
pub fn last_popup() -> Option<PopupRecord> {
st(|s| s.popups.last().cloned())
}
pub fn clear_popup_log() {
st(|s| s.popups.clear());
}
pub fn user_context_menu(id: WidgetId, x: i32, y: i32) {
core::event(id, Event::ContextMenu { x, y })
}
pub fn user_select_row(id: WidgetId, row: Option<usize>) {
user(id, Event::Selected(row))
}
pub fn user_activate_row(id: WidgetId, row: usize) {
core::event(id, Event::Activated(row))
}
pub fn user_click_column(id: WidgetId, col: usize) {
core::event(id, Event::ColumnClicked(col))
}
pub fn user_tree_select(id: WidgetId, node: Option<u64>) {
st(|s| {
if let Some(w) = s.widgets.get_mut(&id) {
w.tree_selected = node;
}
});
core::event(id, Event::TreeSelected(node))
}
pub fn user_tree_activate(id: WidgetId, node: u64) {
core::event(id, Event::TreeActivated(node))
}
pub fn user_tree_expand(id: WidgetId, node: u64, expanded: bool) {
st(|s| {
if let Some(r) = s
.widgets
.get_mut(&id)
.and_then(|w| w.tree_rows.iter_mut().find(|r| r.node == node))
{
r.expanded = expanded;
}
});
core::event(id, Event::TreeExpanded(node, expanded))
}
pub fn set_unsupported(kinds: &[Kind]) {
st(|s| s.unsupported = kinds.to_vec());
}
pub fn sash_of(splitter: WidgetId) -> Option<WidgetId> {
core::read(splitter, |n| n.split().and_then(|s| s.sash)).flatten()
}
pub fn user_drag_sash(splitter: WidgetId, pos: i32) {
if let Some(s) = sash_of(splitter) {
core::event(s, Event::SashDragged(pos));
}
}
pub fn user_drag_sash_by(splitter: WidgetId, delta: i32) {
let Some(s) = sash_of(splitter) else { return };
let Some(w) = widget(s) else { return };
let start = if w.orientation == Some(Orientation::Vertical) {
w.bounds.y
} else {
w.bounds.x
};
core::event(s, Event::SashDragged(start + delta));
}
pub fn user_sash_key(splitter: WidgetId, key: crate::SashKey) {
if let Some(s) = sash_of(splitter) {
core::event(s, Event::SashKey(key));
}
}
pub fn user_move_window(id: WidgetId, x: i32, y: i32) {
st(|s| {
if let Some(w) = s.widgets.get_mut(&id) {
w.position = Some((x, y));
}
});
core::event(id, Event::Moved { x, y });
}
pub fn fail_next_create() {
st(|s| s.fail_create = true);
}
pub fn dump() -> String {
st(|s| {
fn rec(s: &State, id: WidgetId, depth: usize, out: &mut String) {
let Some(w) = s.widgets.get(&id) else { return };
let b = w.bounds;
out.push_str(&format!(
"{}{:?} {:?} [{},{} {}x{}]{}{}\n",
" ".repeat(depth),
w.kind.unwrap_or(Kind::Spacer),
w.text,
b.x,
b.y,
b.w,
b.h,
if w.visible { "" } else { " hidden" },
if w.enabled { "" } else { " disabled" },
));
for c in s
.order
.iter()
.filter(|c| s.widgets.get(c).and_then(|x| x.parent) == Some(id))
{
rec(s, *c, depth + 1, out);
}
}
let mut out = String::new();
for id in s.order.iter().filter(|i| s.widgets[i].parent.is_none()) {
rec(s, *id, 0, &mut out);
}
out
})
}