Use one standard Iris resource bundle

This commit is contained in:
iris committed 2026-09-11 02:09:17 -04:00
1 parent 6dbc800739
commit 7e5a4c0071
16 files changed
+129 -100

No files matched your search

+2 -2
View File
@@ -1,7 +1,7 @@
use crate::prelude::*;
use android_view::{jni::JNIEnv, ndk::event::Keycode};
use super::view::{AndroidAppState, AndroidRsc};
use super::view::AndroidAppState;
/// Hardware/synthesized key handling for the field that currently has
/// focus. Most typing on Android goes through the IME's `InputConnection`
@@ -10,7 +10,7 @@ use super::view::{AndroidAppState, AndroidRsc};
/// the arrow keys on a physical keyboard) plus whatever `unicode_char`
/// reports for a plain key press. Returns whether anything used the event.
pub(super) fn on_key<'local, State: AndroidAppState>(
rsc: &mut AndroidRsc<State>,
rsc: &mut StdRsc<State>,
state: &mut State,
env: &mut JNIEnv<'local>,
key_code: Keycode,
+1 -2
View File
@@ -10,8 +10,7 @@ mod view;
pub use insets::Insets;
pub use render::AndroidRenderer;
pub use view::{
AndroidAppState, AndroidRsc, AndroidUiState, HasAndroidUiState, IrisViewPeer, WindowInsets,
new_peer,
AndroidAppState, AndroidUiState, HasAndroidUiState, IrisViewPeer, WindowInsets, new_peer,
};
/// Registers the extra native methods this backend needs beyond what
+9 -11
View File
@@ -89,8 +89,8 @@ impl AndroidUiState {
}
}
impl<State: 'static> HasRoot<AndroidRsc<State>> for AndroidUiState {
fn set_root(&mut self, rsc: &mut AndroidRsc<State>, root: StrongWidget) {
impl<State: 'static> HasRoot<StdRsc<State>> for AndroidUiState {
fn set_root(&mut self, rsc: &mut StdRsc<State>, root: StrongWidget) {
self.root = Some(crate::overlay::default_overlay_root(rsc, root));
}
}
@@ -101,15 +101,15 @@ pub trait HasAndroidUiState: Sized + 'static {
}
pub trait AndroidAppState: HasAndroidUiState {
fn new(ui_state: AndroidUiState, rsc: &mut AndroidRsc<Self>) -> Self;
fn new(ui_state: AndroidUiState, rsc: &mut StdRsc<Self>) -> Self;
#[allow(unused_variables)]
fn back_pressed(&mut self, rsc: &mut AndroidRsc<Self>) -> bool {
fn back_pressed(&mut self, rsc: &mut StdRsc<Self>) -> bool {
false
}
#[allow(unused_variables)]
fn platform_ready(&mut self, rsc: &mut AndroidRsc<Self>, vm: JavaVM, view: GlobalRef) {}
fn platform_ready(&mut self, rsc: &mut StdRsc<Self>, vm: JavaVM, view: GlobalRef) {}
#[allow(unused_variables)]
fn on_insets_changed(&mut self, rsc: &mut AndroidRsc<Self>, insets: WindowInsets) {}
fn on_insets_changed(&mut self, rsc: &mut StdRsc<Self>, insets: WindowInsets) {}
}
/// Widget-facing insets in physical pixels, decoupled from JNI's integer shape.
@@ -140,15 +140,13 @@ impl WindowInsets {
}
}
pub type AndroidRsc<State> = AppRsc<State>;
/// The `ViewPeer` android-view dispatches every callback to. One per
/// `RustView` instance; `new_peer` (below) builds it and hands the id to
/// Java the same way android-view's own demo does.
pub struct IrisViewPeer<State: AndroidAppState> {
pub(super) rsc: AndroidRsc<State>,
pub(super) rsc: StdRsc<State>,
pub(super) state: State,
task_recv: TaskMsgReceiver<AndroidRsc<State>>,
task_recv: TaskMsgReceiver<StdRsc<State>>,
/// Converts input and Choreographer timestamps onto one monotonic clock.
device_clock: Option<DeviceClock>,
}
@@ -812,7 +810,7 @@ pub fn new_peer<'local, State: AndroidAppState>(
let vm = env.get_java_vm().unwrap();
let global_view = env.new_global_ref(&view.0).unwrap();
let redraw: Arc<dyn RequestRedraw> = Arc::new(AndroidRedrawHandle::new(vm, global_view));
let (mut rsc, task_recv) = AppRsc::new(redraw);
let (mut rsc, task_recv) = StdRsc::new(redraw);
rsc.ui.set_density(content_scale);
let shared = Rc::new(RefCell::new(Shared::default()));
let ui_state = AndroidUiState::new(shared.clone(), content_scale);
+9 -12
View File
@@ -50,8 +50,8 @@ pub struct DesktopUiState {
pub access: AccessTree,
}
impl<State: 'static> HasRoot<DesktopRsc<State>> for DesktopUiState {
fn set_root(&mut self, rsc: &mut DesktopRsc<State>, root: StrongWidget) {
impl<State: 'static> HasRoot<StdRsc<State>> for DesktopUiState {
fn set_root(&mut self, rsc: &mut StdRsc<State>, root: StrongWidget) {
self.root = Some(crate::overlay::default_overlay_root(rsc, root));
}
}
@@ -81,25 +81,22 @@ pub trait HasDesktopUiState: Sized + 'static {
pub trait DesktopAppState: HasDesktopUiState {
type Event = ();
fn new(ui_state: DesktopUiState, rsc: &mut DesktopRsc<Self>, proxy: Proxy<Self::Event>)
-> Self;
fn new(ui_state: DesktopUiState, rsc: &mut StdRsc<Self>, proxy: Proxy<Self::Event>) -> Self;
#[allow(unused_variables)]
fn event(&mut self, event: Self::Event, rsc: &mut DesktopRsc<Self>) {}
fn event(&mut self, event: Self::Event, rsc: &mut StdRsc<Self>) {}
#[allow(unused_variables)]
fn exit(&mut self, rsc: &mut DesktopRsc<Self>) {}
fn exit(&mut self, rsc: &mut StdRsc<Self>) {}
#[allow(unused_variables)]
fn window_event(&mut self, event: WindowEvent, rsc: &mut DesktopRsc<Self>) {}
fn window_event(&mut self, event: WindowEvent, rsc: &mut StdRsc<Self>) {}
fn window_attributes() -> WindowAttributes {
Default::default()
}
}
pub type DesktopRsc<State> = AppRsc<State>;
pub struct DesktopApp<State: DesktopAppState> {
rsc: DesktopRsc<State>,
rsc: StdRsc<State>,
state: State,
task_recv: TaskMsgReceiver<DesktopRsc<State>>,
task_recv: TaskMsgReceiver<StdRsc<State>>,
}
impl<State: DesktopAppState> AppState for DesktopApp<State> {
@@ -118,7 +115,7 @@ impl<State: DesktopAppState> AppState for DesktopApp<State> {
);
window.set_visible(true);
let desktop_state = DesktopUiState::new(window, access_adapter);
let (mut rsc, task_recv) = AppRsc::new(desktop_state.window.clone());
let (mut rsc, task_recv) = StdRsc::new(desktop_state.window.clone());
// Set before building widgets so the first text shape uses the right density.
let scale = content_scale(desktop_state.window.as_ref());
rsc.ui.set_density(scale);
+5 -7
View File
@@ -134,8 +134,8 @@ impl HarnessState {
}
}
impl HasRoot<HarnessRsc> for HarnessState {
fn set_root(&mut self, rsc: &mut HarnessRsc, root: StrongWidget) {
impl HasRoot<StdRsc<HarnessState>> for HarnessState {
fn set_root(&mut self, rsc: &mut StdRsc<HarnessState>, root: StrongWidget) {
self.root = Some(crate::overlay::default_overlay_root(rsc, root));
}
}
@@ -163,14 +163,12 @@ impl OpenUrl for HarnessState {
}
}
pub type HarnessRsc = AppRsc<HarnessState>;
/// A screen running with no window: the widget tree, the frame loop and
/// the pointer, all advanced by the caller. See the module doc.
pub struct Harness {
pub rsc: HarnessRsc,
pub rsc: StdRsc<HarnessState>,
pub state: HarnessState,
task_recv: TaskMsgReceiver<HarnessRsc>,
task_recv: TaskMsgReceiver<StdRsc<HarnessState>>,
redraws: Arc<RedrawCounter>,
cursor: CursorState,
base: Instant,
@@ -185,7 +183,7 @@ impl Harness {
/// `PHONE_SCALE`.
pub fn new(size: Vec2, density: f32) -> Self {
let redraws = Arc::new(RedrawCounter::default());
let (mut rsc, task_recv) = AppRsc::new(redraws.clone());
let (mut rsc, task_recv) = StdRsc::new(redraws.clone());
rsc.ui.set_density(density);
rsc.ui.resize(size);
Self {
+14 -11
View File
@@ -1,4 +1,4 @@
//! Host-independent resources carried by [`AppRsc`].
//! Host-independent resources carried by [`StdRsc`].
pub mod attr;
pub mod event;
@@ -20,8 +20,11 @@ pub use task::*;
use crate::prelude::*;
use std::sync::Arc;
/// Resources shared by every Iris host.
pub struct AppRsc<State: 'static> {
/// Iris's standard resource bundle.
///
/// Built-in hosts use this bundle, but widgets should depend on the narrow
/// resource traits they need so applications can supply a different bundle.
pub struct StdRsc<State: 'static> {
pub ui: Ui,
pub events: EventManager<Self>,
pub tasks: Tasks<Self>,
@@ -29,7 +32,7 @@ pub struct AppRsc<State: 'static> {
_state: std::marker::PhantomData<State>,
}
impl<State> AppRsc<State> {
impl<State> StdRsc<State> {
pub(crate) fn new(redraw: Arc<dyn RequestRedraw>) -> (Self, TaskMsgReceiver<Self>) {
let (tasks, receiver) = Tasks::init(redraw);
(
@@ -49,7 +52,7 @@ impl<State> AppRsc<State> {
}
}
impl<State> UiRsc for AppRsc<State> {
impl<State> UiRsc for StdRsc<State> {
fn ui(&self) -> &Ui {
&self.ui
}
@@ -72,11 +75,11 @@ impl<State> UiRsc for AppRsc<State> {
}
}
impl<State> HasState for AppRsc<State> {
impl<State> HasState for StdRsc<State> {
type State = State;
}
impl<State> HasEvents for AppRsc<State> {
impl<State> HasEvents for StdRsc<State> {
fn events(&self) -> &EventManager<Self> {
&self.events
}
@@ -86,13 +89,13 @@ impl<State> HasEvents for AppRsc<State> {
}
}
impl<State> HasTasks for AppRsc<State> {
impl<State> HasTasks for StdRsc<State> {
fn tasks_mut(&mut self) -> &mut Tasks<Self> {
&mut self.tasks
}
}
impl<State> HasWidgetState for AppRsc<State> {
impl<State> HasWidgetState for StdRsc<State> {
fn widget_state(&self) -> &WidgetState {
&self.state
}
@@ -102,7 +105,7 @@ impl<State> HasWidgetState for AppRsc<State> {
}
}
impl<State, I: RscIdx<AppRsc<State>>> std::ops::Index<I> for AppRsc<State> {
impl<State, I: RscIdx<StdRsc<State>>> std::ops::Index<I> for StdRsc<State> {
type Output = I::Output;
fn index(&self, index: I) -> &Self::Output {
@@ -110,7 +113,7 @@ impl<State, I: RscIdx<AppRsc<State>>> std::ops::Index<I> for AppRsc<State> {
}
}
impl<State, I: RscIdx<AppRsc<State>>> std::ops::IndexMut<I> for AppRsc<State> {
impl<State, I: RscIdx<StdRsc<State>>> std::ops::IndexMut<I> for StdRsc<State> {
fn index_mut(&mut self, index: I) -> &mut Self::Output {
index.get_mut(self)
}
+3 -3
View File
@@ -525,13 +525,13 @@ fn open_stackable_at<Rsc: HasEvents>(
#[cfg(test)]
mod tests {
use super::*;
use crate::harness::{Harness, HarnessRsc, TouchAction};
use crate::harness::{Harness, HarnessState, TouchAction};
use std::{cell::Cell, rc::Rc};
struct EscapeCounter(Rc<Cell<usize>>);
impl Controller<HarnessRsc> for EscapeCounter {
fn command(&mut self, command: Command, _rsc: &mut HarnessRsc) -> CommandResult {
impl Controller<StdRsc<HarnessState>> for EscapeCounter {
fn command(&mut self, command: Command, _rsc: &mut StdRsc<HarnessState>) -> CommandResult {
if command == Command::Escape {
self.0.set(self.0.get() + 1);
CommandResult::Used
+1 -1
View File
@@ -78,7 +78,7 @@ impl<'a, T: 'static> FnOnce<(&'a mut WidgetState,)> for WeakState<T> {
/// What `Rsc[weak_handle]` indexes through -- one impl per kind of handle
/// (a widget, a piece of per-widget state), shared by both backends' `Rsc`
/// types since indexing a widget tree has nothing to do with windowing.
/// `AppRsc` supplies the shared `Index`/`IndexMut` implementation; a blanket
/// `StdRsc` supplies the shared `Index`/`IndexMut` implementation; a blanket
/// implementation over every possible resource type would conflict.
pub trait RscIdx<Rsc> {
type Output;