Skip to main content

FrusApp

Struct FrusApp 

Source
pub struct FrusApp { /* private fields */ }
Expand description

An application: a root component, and how it is dressed.

use frus_shell::FrusApp;
use frus_widgets::{text, Theme, ThemeMode};

let app = FrusApp::from_fn(|_| Box::new(text("hello")))
    .title("Hello")
    .theme(Theme::light())
    .dark_theme(Theme::dark())
    .theme_mode(ThemeMode::System);

How it is dressed — its title, themes, language, zoom — is kept where a component can reach it too, in host::app: what the builders here set, a component changes with the same names (host::app().set_theme_mode(..)), and the shell reads it every frame.

Implementations§

Source§

impl FrusApp

Source

pub fn new(root: impl StatelessWidget) -> Self

An application whose interface is the StatelessWidget root.

Source

pub fn from_fn( root: impl Fn(&BuildContext<'_>) -> Box<dyn Widget> + 'static, ) -> Self

An application whose interface is a function of the build context — hooks may be called in it.

Source

pub fn stateful<W: StatefulWidget + Clone>(root: W) -> Self

An application whose root is a StatefulWidget. It is configuration, made again on every rebuild — hence Clone.

Source

pub fn router(router: GoRouter) -> Self

An application whose pages are the routes of router: the router builds the interface, moves it when told a location, and answers the back gesture.

use frus_shell::FrusApp;
use frus_widgets::{text, GoRoute, GoRouter};

let router = GoRouter::new(vec![
    GoRoute::new("/", |_, _| text("home")),
    GoRoute::new("/about", |_, _| text("about")),
]);
let app = FrusApp::router(router).title("Pages");
Source

pub fn builder( self, builder: impl Fn(&BuildContext<'_>, Box<dyn Widget>) -> Box<dyn Widget> + 'static, ) -> Self

What goes around the application’s pages: builder is handed them, as child, and returns what the window shows. The place for what belongs to the window rather than to a page — what must stay put while the pages change under it, and stay there whichever page is on show: a WindowMenuBar, a banner across every page.

use frus_shell::FrusApp;
use frus_widgets::{
    text, Callback, GoRoute, GoRouter, MenuBar, MenuPath, SubmenuButton, WindowMenuBar,
};

let router = GoRouter::new(vec![GoRoute::new("/", |_, _| text("home"))]);
let app = FrusApp::router(router).builder(|cx, pages| {
    // Which menu is open: the window's to keep, as the bar is.
    let menus = cx.use_state(MenuPath::closed);
    let set = menus.clone();
    let bar = MenuBar::new(&menus.get(), move |path: MenuPath| {
        let set = set.clone();
        Callback::new(move || set.set(path.clone()))
    })
    .menu(SubmenuButton::new("File"));
    Box::new(WindowMenuBar::new(bar, pages))
});

child is built where builder puts it, so what it reads of the window there — a MediaQuery a wrapper narrowed — is what its pages are built for. builder may call hooks, and reach the router with BuildContext::router.

Keep the shape of what it returns: the pages are kept under their place in the tree, and a child that moves loses their state. A wrapper that is sometimes not needed is told so — WindowMenuBar::new(None, child) — rather than left out.

Source

pub fn single_instance(self, id: impl Into<String>) -> Self

Makes the application one window on a desktop, named id — a reverse domain such as com.example.myapp. A second process started with a link (myapp://orders/42, how a desktop opens a registered scheme) hands it to the first, which opens the location it names, and ends. See Application::instance_id.

Source

pub fn location_strategy(self, strategy: LocationStrategy) -> Self

How the router’s location is written in a browser’s address bar: after a # (the default, which any static host serves) or as a path (which needs the server to answer every address with the page). Only the web has an address bar to show it in. See LocationStrategy.

Source

pub fn title(self, title: impl Into<String>) -> Self

The window’s title.

Source

pub fn theme(self, theme: Theme) -> Self

The application’s theme — its light one, where it has two.

Source

pub fn dark_theme(self, theme: Theme) -> Self

The theme for a dark interface. With one, the application follows the system’s brightness, or theme_mode.

Source

pub fn theme_mode(self, mode: ThemeMode) -> Self

Which of the two themes is on display: the system’s choice by default.

Source

pub fn supported_locales(self, locales: Vec<Locale>) -> Self

The languages the application has, best first. The framework resolves the device’s list against them.

Source

pub fn localizations(self, table: Rc<dyn Localizations>) -> Self

The words the framework says on the application’s behalf, where they are not English.

Source

pub fn on_start(self, start: impl FnOnce() + 'static) -> Self

Runs start once, when the application starts and before anything is drawn: the place for what has to be true from the first frame — registering data, opening a store.

Source

pub fn persist( self, save: impl Fn() -> Option<Vec<u8>> + 'static, restore: impl Fn(&[u8]) + 'static, ) -> Self

Keeps what save returns across a live reload, and hands it to restore afterwards. A development convenience: the state of an application survives a recompilation.

Source

pub fn icon(self, icon: AppIcon) -> Self

The application’s icon. The frus logo is the default — an application has it without writing a line — and this is how to change it: .icon(AppIcon::from_png(include_bytes!( "../assets/icon.png"))), or .icon(AppIcon::none()) for none of the framework’s. On Android the launcher icon is the manifest’s instead; see AppIcon.

Source

pub fn window_size(self, width: f32, height: f32) -> Self

The window’s initial size, in logical pixels.

Source

pub fn density(self, density: f32) -> Self

A zoom on the whole interface, on top of the system’s scale.

Trait Implementations§

Source§

impl Application for FrusApp

Source§

fn go_back(&mut self)

The router pops: the page leaves with its transition, as it does for a back button in the reference, and not as a swipe let go (milestone 622).

Source§

type Message = Callback

The message type the interface emits and Application::update consumes. Read more
Source§

fn update(&mut self, callback: Callback) -> Command<Callback>

Advances the state in response to a message, and returns the effects to run: I/O, background tasks and the like. Use Command::none for none.
Source§

fn init(&mut self) -> Command<Callback>

The effect to run at startup, such as an initial load.
Source§

fn effects(&mut self) -> Command<Callback>

Effects the application asked for outside an update — from a build, say, or from a component that has no message to return them with. The shell asks after every frame and after every message, and runs what comes back like any other command. An application that returns its effects from update never needs it.
Source§

fn save_state(&self) -> Option<Vec<u8>>

Live reload, in development: a serialised snapshot of the state, captured just before a recompiled binary relaunches the application (FRUS_WATCH=1). None, the default, means the app opts out, and a reload then starts from a fresh state. The byte format belongs to the app.
Source§

fn restore_state(&mut self, bytes: &[u8])

Rehydrates the state from an Application::save_state snapshot taken by the previous binary; called before Application::init. The bytes come from another version of the code, so tolerate unexpected formats — ignoring beats panicking.
Source§

fn view(&self, _theme: &Theme) -> Box<dyn Widget<Callback>>

Builds the widget tree for the current theme. Read more
Source§

fn subscription(&self) -> Subscription<Callback>

Continuous sources of messages — timers and so on — declared from the state. The framework starts and stops them by diffing every cycle.
Source§

fn tick(&mut self, dt: f32) -> bool

Advances the app’s own animations — a screen transition, a gesture settling — by dt seconds. Returns true while something is still moving, in which case the framework asks for another frame. Read more
Source§

fn location(&self) -> Option<String>

Where the application is, as an address: /users/42?tab=posts. None — the default — for an application with no addresses, which the shell then leaves alone. Read more
Source§

fn location_replaces(&self) -> bool

Whether the move that led to location swapped the page — a router’s replace — rather than adding one. The browser’s entry is then swapped too, instead of a new one being added behind it. false by default.
Source§

fn location_stack(&self) -> Vec<String>

The locations of the pages beneath the one shown, the lowest first, and location last. Used once, when the page is opened at an address deep in the application: the browser’s history is made to hold the pages beneath too, so that going back — the application’s own or the browser’s — has somewhere to land inside the application. The default is the location alone.
Source§

fn open_location(&mut self, location: &str) -> Command<Callback>

A location was asked for from outside: the address the page was opened at, or the one the browser’s back and forward buttons landed on. Move to it — or to wherever it redirects, and the shell corrects the address bar to match. Called with a location the application may already be at; that is a no-op to do, not to report. Read more
Source§

fn location_strategy(&self) -> LocationStrategy

How location is written in the address bar. See LocationStrategy.
Source§

fn instance_id(&self) -> Option<String>

Whether the application is one window, and its name: on a desktop, a second process started with a link — the operating system’s way of opening myapp://orders/42 — hands the link to the first and ends, instead of opening a second window. None, the default, lets every process be its own. Read more
Source§

fn can_go_back(&self) -> bool

Can the app go back? This enables the edge back gesture.
Source§

fn back_gesture(&mut self, progress: f32)

The back gesture is progressing (0 → 1), so view can preview it.
Source§

fn back_gesture_end(&mut self, velocity: f32)

The back gesture was released, with the finger’s velocity in fractions per second. It is up to the app to commit or cancel, usually through an animated settle in tick.
Source§

fn theme(&self) -> Theme

The application’s theme — its light one, where it has two (app.dart:425). Read more
Source§

fn dark_theme(&self) -> Option<Theme>

The theme to use when a dark interface is asked for (app.dart:447). Read more
Source§

fn theme_mode(&self) -> ThemeMode

Which of the two themes is on display (app.dart:502): the platform’s choice by default, or one of them pinned. Read more
Source§

fn supported_locales(&self) -> Vec<Locale>

The languages this application has (app.dart’s supportedLocales), best first — the answer to what can I actually show this reader? Read more
Source§

fn locale(&self) -> Option<Locale>

A language pinned by the application, over the reader’s own (app.dart’s locale); None — the default — follows the device. Read more
Source§

fn localizations(&self) -> Option<Rc<dyn Localizations>>

The words the framework says on this application’s behalf, in the reader’s language: the label on a back arrow, the word on a notification’s cross, a calendar’s month names and which day its weeks start on. Read more
Source§

fn title(&self) -> String

The window’s title.
Source§

fn window_size(&self) -> Option<(f32, f32)>

The window’s desired initial logical size; None for the system default. Ignored on the Web, where the page decides how big the canvas is.
Source§

fn icon(&self) -> AppIcon

The application’s icon: the frus logo unless it says otherwise. On the desktop it is the window’s, on the Web the tab’s; see AppIcon for what it does on each platform, and for how to change or remove it.
Source§

fn density(&self) -> f32

The interface’s density: an application-level zoom factor, 1.0 by default, applied on top of the system DPI scale. 1.2 grows the whole UI by 20%, 0.9 tightens it. It can change at runtime through the state.
Source§

fn high_contrast_theme(&self) -> Option<Theme>

The theme to use when the platform asks for higher contrast (app.dart:460); None falls back to Application::theme. Read more
Source§

fn high_contrast_dark_theme(&self) -> Option<Theme>

The theme for a dark, high-contrast interface (app.dart:476); None falls back to Application::dark_theme, then to Application::high_contrast_theme, then to Application::theme — the reference’s own order, rung for rung.
Source§

fn resolved_theme(&self, brightness: Brightness, high_contrast: bool) -> Theme

Which of this application’s themes is on display, given what the platform reports — the reference’s _themeBuilder (app.dart:995), rung for rung. Read more
Source§

fn theme_animation_duration(&self) -> f32

How long the framework takes to cross from one theme to the next, in seconds (app.dart:516); 0 switches at once. The default is the reference’s 200 ms. Read more
Source§

fn theme_animation_curve(&self) -> Curve

The shape of that crossing (app.dart:526); linear by default, as in the reference. Ignored when Application::theme_animation_duration is 0.
Source§

fn accessibility(&self) -> AccessibilityOverrides

What this application wants to say about the accessibility settings instead of the platform. Read more
Source§

fn scroll_behavior(&self) -> ScrollBehavior

How the application’s scrollables behave: their physics, their scrollbars, what they show past an edge and how they read a fling, all from one frus_widgets::ScrollBehavior put in force at the root of the tree. Read more
Source§

fn resolved_locale(&self, preferred: &[Locale]) -> Locale

Which language the interface ends up in, given what the reader prefers. Read more
Source§

fn on_resize(&mut self, _width: f32, _height: f32)

Called when the surface’s logical size changes, whether the window was resized or the density changed. It lets the app react to a breakpoint change in its own logic — closing a drawer as things narrow, say — beyond simply re-rendering.
Source§

fn on_lifecycle(&mut self, _state: Lifecycle)

Called at every lifecycle transition (see Lifecycle): Resumed in the foreground, Inactive when the window loses focus, Paused in the background with the surface lost, Detached just before closing. The default does nothing. This is where an app suspends and resumes its work — timers, sensors — or persists its state.
Source§

fn on_insets(&mut self, _insets: WindowInsets)

Called when the window insets change, split by nature: padding is the system bars and the notch, which are static, while view_insets is the software keyboard, which is dynamic — avoiding the keyboard means pushing the content away by insets.safe(). In logical px. Always zero on desktop.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<Msg> IntoMsg<Msg> for Msg

Source§

fn defer(work: impl Fn() -> Msg + 'static) -> Msg

The message that stands for work’s result.
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more