mod backend;
pub mod controls;
mod page_a11y;
#[path = "styles/recipe_web_view_style.rs"]
mod recipe_web_view_style;
#[cfg(feature = "wry-backend")]
mod wry_backend;
#[cfg(feature = "wry-backend")]
pub use wry_backend::WryBackend;
#[cfg(feature = "servo-backend")]
mod servo_backend;
#[cfg(feature = "servo-backend")]
pub use servo_backend::ServoBackend;
pub use backend::{
ConsoleLevel, MEMORY_SURFACE_COLOUR, MemoryWebViewBackend, MemoryWebViewRecords,
NoopWebViewBackend, WebSource, WebViewAttributes, WebViewBackend, WebViewEditCommand,
WebViewEvent, WebViewEventPayload, WebViewHandle, WebViewId, WebViewInputEvent, WebViewOp,
WebViewPumpPayload, WebViewRegistry, WebViewRendering, WebViewTouchPhase, memory_registry,
memory_surface_registry,
};
pub use controls::{
SharedWebViewControlPresenter, WebViewControl, WebViewControlAnswer, WebViewControlPresenter,
WebViewSelectOption,
};
pub use page_a11y::PageAccessibilityUpdate;
pub use recipe_web_view_style::RecipeWebViewStyle;
pub use teksilo_core::styles::{
SharedWebViewStyle, WebViewStyle, WebViewStyleConfig, WebViewVisualState,
};
pub fn is_wayland() -> bool {
match std::env::var("WINIT_UNIX_BACKEND") {
Ok(b) if b.eq_ignore_ascii_case("wayland") => return true,
Ok(b) if b.eq_ignore_ascii_case("x11") => return false,
_ => {}
}
std::env::var_os("WAYLAND_DISPLAY").is_some_and(|v| !v.is_empty())
}
#[cfg(all(target_os = "linux", feature = "wry-backend"))]
pub fn pump_gtk_events() {
if gtk::is_initialized() {
while gtk::events_pending() {
gtk::main_iteration_do(false);
}
}
}
#[cfg(not(all(target_os = "linux", feature = "wry-backend")))]
pub fn pump_gtk_events() {}
use std::cell::{Cell, RefCell};
use std::rc::Rc;
use teksilo_canvas::live_image::{LiveImageDraw, LiveImageSource, LivePixelFormat};
use teksilo_canvas::{
Canvas, ImageGeometry, ImageOrientation, Point, Rect, SizeProposal, Transform2D,
};
use teksilo_core::accessibility::AccessNodeBuilder;
use teksilo_core::accesskit::Role;
use teksilo_core::build_context::BuildContext;
use teksilo_core::signal::Signal;
use teksilo_core::widget::{
EventContext, LayoutContext, LayoutResponse, PaintContext, Widget, WidgetPlacement,
WidgetTreeView,
};
use teksilo_core::widget_id::WidgetId;
use teksilo_core::window::TeksiloWindowId;
use teksilo_core::{LiveImageAttachment, LiveImageSignals};
type MessageCallback = Rc<RefCell<dyn FnMut(String, &mut EventContext)>>;
type TitleCallback = Rc<RefCell<dyn FnMut(String, &mut EventContext)>>;
type NavigationCallback = Rc<RefCell<dyn FnMut(NavigationInfo, &mut EventContext)>>;
type PageLoadCallback = Rc<RefCell<dyn FnMut(PageLoadState, &mut EventContext)>>;
type DownloadStartCallback = Rc<RefCell<dyn FnMut(DownloadStart, &mut EventContext)>>;
type DownloadFinishCallback = Rc<RefCell<dyn FnMut(DownloadOutcome, &mut EventContext)>>;
type ConsoleCallback = Rc<RefCell<dyn FnMut(ConsoleLevel, String, &mut EventContext)>>;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PageLoadState {
Started,
Finished,
}
#[derive(Debug, Clone)]
pub struct NavigationInfo {
pub url: String,
pub can_cancel: bool,
}
#[derive(Debug, Clone)]
pub struct DownloadStart {
pub url: String,
pub suggested_path: std::path::PathBuf,
}
#[derive(Debug, Clone)]
pub struct DownloadOutcome {
pub path: std::path::PathBuf,
pub success: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum WebViewInput {
#[default]
Native,
Transparent,
}
impl WebViewInput {
pub fn is_native(self) -> bool {
matches!(self, WebViewInput::Native)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct EngineVisibility {
active: bool,
in_view: bool,
uncovered: bool,
}
impl EngineVisibility {
const VISIBLE: Self = Self {
active: true,
in_view: true,
uncovered: true,
};
fn resolved(self) -> bool {
self.active && self.in_view && self.uncovered
}
}
struct SurfaceView {
source: LiveImageSource,
signals: LiveImageSignals,
orientation: ImageOrientation,
attachment: RefCell<Option<LiveImageAttachment>>,
geometry: Cell<Option<ImageGeometry>>,
contacts: RefCell<Vec<teksilo_core::pointer::PointerId>>,
last_pointer: Cell<Option<Point>>,
cursor: Cell<Option<teksilo_core::widget::CursorIcon>>,
window_bounds: Cell<Rect>,
}
impl SurfaceView {
fn new(orientation: ImageOrientation) -> Self {
let source = LiveImageSource::builder(LivePixelFormat::Rgbx8)
.label("web view")
.build();
Self {
signals: LiveImageSignals::new(&source),
source,
orientation,
attachment: RefCell::new(None),
geometry: Cell::new(None),
contacts: RefCell::new(Vec::new()),
last_pointer: Cell::new(None),
cursor: Cell::new(None),
window_bounds: Cell::new(Rect::new(0.0, 0.0, 0.0, 0.0)),
}
}
fn place(&self, size: teksilo_canvas::Size, scale: f32) -> Option<ImageGeometry> {
let (w, h) = self.signals.frame_size.get()?;
let (dw, dh) = self.orientation.displayed_size(w, h);
let content = Rect::new(0.0, 0.0, dw as f32 / scale, dh as f32 / scale);
let bounds = Rect::new(0.0, 0.0, size.width, size.height);
Some(ImageGeometry::new(
(w, h),
self.orientation,
content,
bounds,
))
}
fn window_rect(&self, rect: Rect) -> Option<Rect> {
let placed = self.geometry.get()?;
let (w, h) = placed.displayed();
if w == 0 || h == 0 {
return None;
}
let sx = placed.content.width / w as f32;
let sy = placed.content.height / h as f32;
let origin = self.window_bounds.get();
Some(Rect::new(
origin.x + placed.content.x + rect.x * sx,
origin.y + placed.content.y + rect.y * sy,
rect.width * sx,
rect.height * sy,
))
}
fn page_point(&self, local: Point) -> Option<Point> {
let placed = self.geometry.get()?;
let upright = ImageGeometry::new(
placed.displayed(),
ImageOrientation::Normal,
placed.content,
placed.bounds,
);
upright
.map_to_source_f32(local)
.map(|(x, y)| Point::new(x, y))
}
}
type SharedHandle = Rc<RefCell<Option<Box<dyn WebViewHandle>>>>;
pub struct WebView {
attrs: WebViewAttributes,
web_view_id: WebViewId,
handle: SharedHandle,
last_bounds: Rc<Cell<Option<Rect>>>,
scale: Rc<Cell<f32>>,
mount_queued: Cell<bool>,
window_id: Cell<Option<TeksiloWindowId>>,
self_id: Cell<Option<WidgetId>>,
style_override: Option<SharedWebViewStyle>,
root_child_id: Option<WidgetId>,
state_signal: Signal<WebViewVisualState>,
registry: Rc<RefCell<Option<WebViewRegistry>>>,
focused: Signal<bool>,
enter_page_on_focus: bool,
input: WebViewInput,
page_focused: Signal<bool>,
visibility: Rc<Cell<EngineVisibility>>,
visible_applied: Rc<Cell<bool>>,
surface: Option<Rc<SurfaceView>>,
page_tree: Rc<RefCell<page_a11y::PageTree>>,
url_signal: Option<Signal<String>>,
title_signal: Option<Signal<String>>,
loading_signal: Option<Signal<bool>>,
nav_guard: Rc<RefCell<Option<String>>>,
on_message: Option<MessageCallback>,
on_title_changed: Option<TitleCallback>,
on_navigation: Option<NavigationCallback>,
on_page_load: Option<PageLoadCallback>,
on_download_started: Option<DownloadStartCallback>,
on_download_finished: Option<DownloadFinishCallback>,
on_console_message: Option<ConsoleCallback>,
}
impl std::fmt::Debug for WebView {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("WebView")
.field("web_view_id", &self.web_view_id)
.field("opened", &self.handle.borrow().is_some())
.field("source", &self.attrs.source)
.finish_non_exhaustive()
}
}
impl Default for WebView {
fn default() -> Self {
Self::new()
}
}
impl WebView {
pub fn new() -> Self {
Self {
attrs: WebViewAttributes::default(),
web_view_id: WebViewId::next(),
handle: Rc::new(RefCell::new(None)),
last_bounds: Rc::new(Cell::new(None)),
scale: Rc::new(Cell::new(1.0)),
mount_queued: Cell::new(false),
window_id: Cell::new(None),
self_id: Cell::new(None),
style_override: None,
root_child_id: None,
state_signal: Signal::new(WebViewVisualState::Loading),
registry: Rc::new(RefCell::new(None)),
focused: Signal::new(false),
enter_page_on_focus: false,
input: WebViewInput::default(),
page_focused: Signal::new(false),
visibility: Rc::new(Cell::new(EngineVisibility::VISIBLE)),
visible_applied: Rc::new(Cell::new(true)),
surface: None,
page_tree: Rc::new(RefCell::new(page_a11y::PageTree::default())),
url_signal: None,
title_signal: None,
loading_signal: None,
nav_guard: Rc::new(RefCell::new(None)),
on_message: None,
on_title_changed: None,
on_navigation: None,
on_page_load: None,
on_download_started: None,
on_download_finished: None,
on_console_message: None,
}
}
pub fn url(mut self, url: impl Into<String>) -> Self {
self.attrs.source = Some(WebSource::Url(url.into()));
self
}
pub fn html(mut self, html: impl Into<String>) -> Self {
self.attrs.source = Some(WebSource::Html {
html: html.into(),
base_url: None,
});
self
}
pub fn source(mut self, source: WebSource) -> Self {
self.attrs.source = Some(source);
self
}
pub fn user_agent(mut self, ua: impl Into<String>) -> Self {
self.attrs.user_agent = Some(ua.into());
self
}
pub fn transparent(mut self, transparent: bool) -> Self {
self.attrs.transparent = transparent;
self
}
pub fn devtools(mut self, devtools: bool) -> Self {
self.attrs.devtools = devtools;
self
}
pub fn custom_protocol(mut self, scheme: impl Into<String>) -> Self {
self.attrs.custom_protocols.push(scheme.into());
self
}
pub fn url_signal(mut self, signal: Signal<String>) -> Self {
self.url_signal = Some(signal);
self
}
pub fn title_signal(mut self, signal: Signal<String>) -> Self {
self.title_signal = Some(signal);
self
}
pub fn loading_signal(mut self, signal: Signal<bool>) -> Self {
self.loading_signal = Some(signal);
self
}
pub fn on_message(mut self, cb: impl FnMut(String, &mut EventContext) + 'static) -> Self {
self.on_message = Some(Rc::new(RefCell::new(cb)));
self
}
pub fn on_title_changed(mut self, cb: impl FnMut(String, &mut EventContext) + 'static) -> Self {
self.on_title_changed = Some(Rc::new(RefCell::new(cb)));
self
}
pub fn on_navigation(
mut self,
cb: impl FnMut(NavigationInfo, &mut EventContext) + 'static,
) -> Self {
self.on_navigation = Some(Rc::new(RefCell::new(cb)));
self
}
pub fn on_page_load(
mut self,
cb: impl FnMut(PageLoadState, &mut EventContext) + 'static,
) -> Self {
self.on_page_load = Some(Rc::new(RefCell::new(cb)));
self
}
pub fn on_download_started(
mut self,
cb: impl FnMut(DownloadStart, &mut EventContext) + 'static,
) -> Self {
self.on_download_started = Some(Rc::new(RefCell::new(cb)));
self
}
pub fn on_download_finished(
mut self,
cb: impl FnMut(DownloadOutcome, &mut EventContext) + 'static,
) -> Self {
self.on_download_finished = Some(Rc::new(RefCell::new(cb)));
self
}
pub fn on_console_message(
mut self,
cb: impl FnMut(ConsoleLevel, String, &mut EventContext) + 'static,
) -> Self {
self.on_console_message = Some(Rc::new(RefCell::new(cb)));
self
}
pub fn style(mut self, style: impl WebViewStyle) -> Self {
self.style_override = Some(Rc::new(style));
self
}
pub fn enter_page_on_focus(mut self, enter: bool) -> Self {
self.enter_page_on_focus = enter;
self
}
pub fn input_mode(mut self, input: WebViewInput) -> Self {
self.input = input;
self
}
pub fn page_focused_signal(&self) -> Signal<bool> {
self.page_focused.clone()
}
pub fn focus_page(&self) {
self.with_handle(|h| h.set_focus());
}
pub fn focused_signal(&self) -> Signal<bool> {
self.focused.clone()
}
pub fn id(&self) -> WebViewId {
self.web_view_id
}
pub fn load_url(&self, url: &str) {
self.with_handle(|h| h.load_url(url));
}
pub fn post_message(&self, msg: &str) {
self.with_handle(|h| h.post_message(msg));
}
pub fn eval(&self, script: &str) {
self.with_handle(|h| h.eval(script));
}
pub fn reload(&self) {
self.with_handle(|h| h.reload());
}
pub fn go_back(&self) {
self.with_handle(|h| h.go_back());
}
pub fn go_forward(&self) {
self.with_handle(|h| h.go_forward());
}
pub fn stop(&self) {
self.with_handle(|h| h.stop());
}
pub fn open_devtools(&self) {
self.with_handle(|h| h.open_devtools());
}
pub fn close_devtools(&self) {
self.with_handle(|h| h.close_devtools());
}
fn visible_rect(&self, bounds: Rect, ctx: &LayoutContext) -> Option<Rect> {
let (Some(arena), Some(id)) = (ctx.arena(), self.self_id.get()) else {
return Some(bounds);
};
let mut rect = bounds;
let mut cursor = arena.parent(id);
while let Some(ancestor) = cursor {
if arena.get(ancestor).is_some_and(|node| node.clips_children) {
rect = intersect(rect, arena.bounds(ancestor))?;
}
cursor = arena.parent(ancestor);
}
Some(rect)
}
fn place_surface(&self, surface: &SurfaceView, bounds: Rect, scale: f32, ctx: &LayoutContext) {
let scale = usable_scale(scale);
let ancestors = ctx
.arena()
.zip(self.self_id.get())
.map(|(arena, id)| arena.effective_transform(id))
.unwrap_or(Transform2D::IDENTITY);
let to_device = ancestors.then(&Transform2D::scale(scale, scale));
let placed = surface
.place(bounds.size(), scale)
.map(|g| g.snapped(Transform2D::translate(bounds.x, bounds.y).then(&to_device)));
if let Some(attachment) = surface.attachment.borrow().as_ref() {
attachment.set_snapped_under(Some(ancestors));
if let Some(g) = placed {
attachment.set_geometry(g);
}
}
surface.geometry.set(placed);
surface.window_bounds.set(bounds);
}
fn with_handle(&self, f: impl FnOnce(&dyn WebViewHandle)) {
if let Some(h) = self.handle.borrow().as_ref() {
f(h.as_ref());
}
}
fn make_event_callback(
&self,
self_id: WidgetId,
) -> impl FnMut(WebViewEvent, &mut EventContext) + 'static {
let page_focused = self.page_focused.clone();
let url_signal = self.url_signal.clone();
let title_signal = self.title_signal.clone();
let loading_signal = self.loading_signal.clone();
let state_signal = self.state_signal.clone();
let nav_guard = self.nav_guard.clone();
let on_message = self.on_message.clone();
let on_title_changed = self.on_title_changed.clone();
let on_navigation = self.on_navigation.clone();
let on_page_load = self.on_page_load.clone();
let on_download_started = self.on_download_started.clone();
let on_download_finished = self.on_download_finished.clone();
let on_console_message = self.on_console_message.clone();
let surface = self.surface.clone();
let control_handle = self.handle.clone();
let page_tree = self.page_tree.clone();
move |event, ctx| match event {
WebViewEvent::PageLoadStarted => {
if let Some(s) = &loading_signal {
s.set(true);
}
state_signal.set(WebViewVisualState::Loading);
if let Some(cb) = &on_page_load {
(cb.borrow_mut())(PageLoadState::Started, ctx);
}
}
WebViewEvent::PageLoadFinished => {
if let Some(s) = &loading_signal {
s.set(false);
}
state_signal.set(WebViewVisualState::Ready);
if let Some(cb) = &on_page_load {
(cb.borrow_mut())(PageLoadState::Finished, ctx);
}
}
WebViewEvent::NavigationStarted { url, can_cancel } => {
if let Some(cb) = &on_navigation {
(cb.borrow_mut())(NavigationInfo { url, can_cancel }, ctx);
}
}
WebViewEvent::NavigationFinished { url, success } => {
if success {
*nav_guard.borrow_mut() = Some(url.clone());
if let Some(s) = &url_signal {
s.set(url);
}
state_signal.set(WebViewVisualState::Ready);
} else {
state_signal.set(WebViewVisualState::Error);
}
}
WebViewEvent::TitleChanged(title) => {
if let Some(s) = &title_signal {
s.set(title.clone());
}
if let Some(cb) = &on_title_changed {
(cb.borrow_mut())(title, ctx);
}
}
WebViewEvent::Message(msg) => {
if let Some(cb) = &on_message {
(cb.borrow_mut())(msg, ctx);
}
}
WebViewEvent::DownloadStarted {
url,
suggested_path,
} => {
if let Some(cb) = &on_download_started {
(cb.borrow_mut())(
DownloadStart {
url,
suggested_path,
},
ctx,
);
}
}
WebViewEvent::DownloadFinished { path, success } => {
if let Some(cb) = &on_download_finished {
(cb.borrow_mut())(DownloadOutcome { path, success }, ctx);
}
}
WebViewEvent::ConsoleMessage { level, text } => {
if let Some(cb) = &on_console_message {
(cb.borrow_mut())(level, text, ctx);
}
}
WebViewEvent::CursorChanged(cursor) => {
if let Some(surface) = &surface {
surface.cursor.set(Some(cursor));
}
}
WebViewEvent::ControlRequested { key, mut control } => {
if let WebViewControl::Select { area, .. } = &mut control
&& let Some(window) = surface.as_ref().and_then(|s| s.window_rect(*area))
{
*area = window;
}
let answer_handle = control_handle.clone();
let reply: controls::WebViewControlReply = Box::new(move |answer| {
if let Some(h) = answer_handle.borrow().as_ref() {
h.answer_control(key, answer);
}
});
match ctx.app_state::<SharedWebViewControlPresenter>().cloned() {
Some(presenter) => presenter.0.present(control, ctx, reply),
None => reply(WebViewControlAnswer::Dismissed),
}
}
WebViewEvent::PageAccessibility(update) => {
page_tree.borrow_mut().apply(update);
ctx.request_accessibility_update();
}
WebViewEvent::InputMethodArea(area) => {
if let Some(area) = area.and_then(|a| surface.as_ref()?.window_rect(a)) {
ctx.set_ime_cursor_area(area);
}
}
WebViewEvent::EngineFocusChanged(has_focus) => {
page_focused.set(has_focus);
if has_focus && ctx.focused() != Some(self_id) {
ctx.request_focus(self_id);
}
}
}
}
}
impl Widget for WebView {
fn build(&mut self, ctx: &mut BuildContext) -> Vec<WidgetId> {
let self_id = ctx.self_id();
let style = self
.style_override
.clone()
.or_else(|| ctx.theme().style_slots.web_view.clone())
.unwrap_or_else(|| Rc::new(RecipeWebViewStyle));
let content = ctx.add(EmptyOverlayContent);
let body = style.make_body(
&WebViewStyleConfig {
state: self.state_signal.clone(),
focused: self.focused.clone(),
content,
},
ctx,
);
self.root_child_id = Some(body);
self.self_id.set(Some(self_id));
let rendering = ctx
.app_state::<WebViewRegistry>()
.map(WebViewRegistry::rendering)
.unwrap_or(WebViewRendering::NativeSubview);
if let WebViewRendering::Surface { orientation } = rendering {
let surface = self
.surface
.get_or_insert_with(|| Rc::new(SurfaceView::new(orientation)))
.clone();
let attachment = ctx.attach_live_image(&surface.source, &surface.signals);
*surface.attachment.borrow_mut() = Some(attachment);
let registry = ctx.binding_registry();
surface.signals.frame_size.bind_to(
self_id,
registry,
teksilo_core::binding::BindingLevel::Relayout,
);
surface.signals.status.bind_to(
self_id,
registry,
teksilo_core::binding::BindingLevel::RepaintOnly,
);
}
let focused = self.focused.clone();
let focus_handle = self.handle.clone();
let enter_on_focus = self.enter_page_on_focus;
let blur_page_focused = self.page_focused.clone();
let blurs = self.surface.is_some();
let mut handlers = teksilo_core::widget_builder::HandlerSet::new()
.focusable(true)
.on_focus(move |gained, _ctx| {
focused.set(gained);
if gained
&& enter_on_focus
&& let Some(h) = focus_handle.borrow().as_ref()
{
h.set_focus();
}
if !gained && blurs {
blur_page_focused.set(false);
if let Some(h) = focus_handle.borrow().as_ref() {
h.blur();
}
}
});
if self.surface.is_some() {
handlers = handlers.ime_input(teksilo_core::ime::ImeContext::text());
}
let key_handle = self.handle.clone();
let key_page_focused = self.page_focused.clone();
let key_forwards = self.surface.is_some();
handlers = handlers.on_key(move |event, _ctx| {
use teksilo_core::event::{EventResponse, Key, Modifiers, WidgetEvent};
if key_forwards
&& key_page_focused.get()
&& let Some(input) = forwarded_key(event)
&& let Some(h) = key_handle.borrow().as_ref()
{
h.send_input(&input);
return EventResponse::Handled;
}
if let WidgetEvent::KeyDown { key, modifiers, .. } = event
&& matches!(key, Key::Enter | Key::Space)
&& *modifiers == Modifiers::NONE
&& let Some(h) = key_handle.borrow().as_ref()
{
h.set_focus();
return EventResponse::Handled;
}
EventResponse::Ignored
});
let action_handle = self.handle.clone();
handlers = handlers.on_access_action(move |action, _ctx| {
use teksilo_core::event::EventResponse;
if matches!(
action,
teksilo_core::accesskit::Action::Click | teksilo_core::accesskit::Action::Focus
) && let Some(h) = action_handle.borrow().as_ref()
{
h.set_focus();
return EventResponse::Handled;
}
EventResponse::Ignored
});
if let Some(surface) = self.surface.clone() {
handlers = forward_surface_pointer(handlers, surface, self.handle.clone(), self.input);
} else if self.input.is_native() {
use teksilo_core::event::EventResponse;
use teksilo_core::pointer::CancelReason;
use teksilo_core::pointer::touch_action::TouchAction;
handlers = handlers
.touch_action(TouchAction::NONE)
.no_hit_slop()
.on_pointer_event(move |event, ctx| {
use teksilo_core::event::WidgetEvent;
match event {
WidgetEvent::PointerDown { .. } | WidgetEvent::PointerUp { .. } => {
ctx.cancel_pointer_sequence(CancelReason::Deactivated);
EventResponse::Handled
}
WidgetEvent::PointerMove { .. } => {
if ctx.press_is_inside() {
ctx.cancel_pointer_sequence(CancelReason::Deactivated);
}
EventResponse::Handled
}
_ => EventResponse::Ignored,
}
});
}
ctx.apply_self_handlers(handlers);
if self.surface.is_some() {
use teksilo_core::shortcut::StandardEdit;
for (command, edit) in [
(StandardEdit::Copy, WebViewEditCommand::Copy),
(StandardEdit::Cut, WebViewEditCommand::Cut),
(StandardEdit::Paste, WebViewEditCommand::Paste),
(StandardEdit::PastePlain, WebViewEditCommand::Paste),
] {
let handle = self.handle.clone();
let page_focused = self.page_focused.clone();
ctx.register_action(
teksilo_core::action::Action::new(command.id()).on_invoke_with_response(
move |_intent, _ctx| {
if page_focused.get()
&& let Some(h) = handle.borrow().as_ref()
{
h.send_input(&WebViewInputEvent::Edit(edit));
teksilo_core::intent::IntentResponse::Handled
} else {
teksilo_core::intent::IntentResponse::Propagated
}
},
),
);
}
}
self.window_id.set(ctx.window().map(|w| w.id()));
ctx.device_scale_signal().bind_to(
self_id,
ctx.binding_registry(),
teksilo_core::binding::BindingLevel::Relayout,
);
let vis = ctx.activation_signal(self_id);
let effect_handle = self.handle.clone();
let effect_visibility = self.visibility.clone();
let effect_applied = self.visible_applied.clone();
ctx.effect(&vis, move |active| {
let mut state = effect_visibility.get();
state.active = *active;
effect_visibility.set(state);
apply_visibility(&effect_handle, &effect_visibility, &effect_applied);
});
if let Some(url_signal) = self.url_signal.clone() {
*self.nav_guard.borrow_mut() = Some(url_signal.get());
let nav_guard = self.nav_guard.clone();
let nav_handle = self.handle.clone();
ctx.effect(&url_signal, move |url| {
if nav_guard.borrow().as_deref() == Some(url.as_str()) {
return;
}
*nav_guard.borrow_mut() = Some(url.clone());
if let Some(h) = nav_handle.borrow().as_ref() {
h.load_url(url);
}
});
}
if !self.mount_queued.get() {
self.mount_queued.set(true);
let web_view_id = self.web_view_id;
let window_id = self.window_id.get();
let attrs = self.attrs.clone();
let handle_slot = self.handle.clone();
let bounds_slot = self.last_bounds.clone();
let scale_slot = self.scale.clone();
let registry_slot = self.registry.clone();
let activation = vis;
let on_event = self.make_event_callback(self_id);
let input = self.input;
let visibility = self.visibility.clone();
let visible_applied = self.visible_applied.clone();
let surface = self.surface.clone();
ctx.run_after_mount(move |ectx| {
if handle_slot.borrow().is_some() {
return;
}
let Some(registry) = ectx.app_state::<WebViewRegistry>().cloned() else {
return;
};
let parent = ectx.parent_window_handle();
let poster = ectx.poster().cloned();
let wid = window_id.unwrap_or_else(|| TeksiloWindowId::new(0));
let writer = surface.as_ref().map(|s| s.source.writer());
let handle =
registry.open(web_view_id, wid, parent, attrs, poster, writer, on_event);
if let Some(b) = bounds_slot.get() {
handle.set_bounds(b, scale_slot.get());
}
if input == WebViewInput::Transparent && surface.is_none() {
handle.set_input_passthrough(true);
}
*handle_slot.borrow_mut() = Some(handle);
*registry_slot.borrow_mut() = Some(registry);
let mut state = visibility.get();
state.active = activation.get();
visibility.set(state);
apply_visibility(&handle_slot, &visibility, &visible_applied);
});
}
self.children()
}
fn layout_response(&self, proposal: SizeProposal, ctx: &LayoutContext) -> LayoutResponse {
self.root_child_id
.and_then(|id| ctx.child_size(id, proposal))
.unwrap_or_else(|| proposal.resolve(0.0, 0.0))
.into()
}
fn place_children(
&self,
bounds: Rect,
_proposal: SizeProposal,
children: &mut [WidgetPlacement],
ctx: &LayoutContext,
) {
for child in children.iter_mut() {
child.origin = bounds.origin();
child.size = bounds.size();
}
let scale = ctx.scale_factor;
let scale_changed = (self.scale.get() - scale).abs() > f32::EPSILON;
if scale_changed {
self.scale.set(scale);
}
let visible = self.visible_rect(bounds, ctx);
let mut state = self.visibility.get();
state.in_view = visible.is_some();
self.visibility.set(state);
let target = match &self.surface {
Some(surface) => {
self.place_surface(surface, bounds, scale, ctx);
Some(bounds)
}
None => visible,
};
if let Some(rect) = target
&& (self.last_bounds.get() != Some(rect) || scale_changed)
{
self.last_bounds.set(Some(rect));
self.with_handle(|h| h.set_bounds(rect, scale));
}
apply_visibility(&self.handle, &self.visibility, &self.visible_applied);
}
fn wants_after_paint(&self) -> bool {
self.surface.is_none()
}
fn wants_post_paint(&self) -> bool {
self.surface.is_some()
}
fn post_paint(&self, bounds: Rect, canvas: &mut Canvas, _ctx: &PaintContext) {
let Some(surface) = &self.surface else {
return;
};
let attachment = surface.attachment.borrow();
let Some(attachment) = attachment.as_ref() else {
return;
};
let content = surface
.geometry
.get()
.map(|g| {
Rect::new(
bounds.x + g.content.x,
bounds.y + g.content.y,
g.content.width,
g.content.height,
)
})
.unwrap_or(Rect::new(bounds.x, bounds.y, 0.0, 0.0));
canvas.draw_live_image(
attachment.consumer(),
&LiveImageDraw::new(content, bounds)
.orientation(surface.orientation)
.pixel_snap(true),
);
}
fn after_paint(&self, view: &WidgetTreeView<'_>, _ctx: &PaintContext) {
let Some(id) = self.self_id.get() else {
return;
};
let bounds = view.bounds(id);
let covered = view
.overlay_rects()
.iter()
.any(|r| intersect(*r, bounds).is_some());
let uncovered = !covered;
let mut state = self.visibility.get();
if state.uncovered == uncovered {
return;
}
state.uncovered = uncovered;
self.visibility.set(state);
apply_visibility(&self.handle, &self.visibility, &self.visible_applied);
}
fn accessibility(&self, builder: &mut AccessNodeBuilder) {
builder.set_role(Role::WebView);
if let Some(title) = &self.title_signal {
builder.set_name(title.get());
}
builder.add_action(teksilo_core::accesskit::Action::Focus);
builder.add_action(teksilo_core::accesskit::Action::Click);
if !self.enter_page_on_focus {
builder.set_keyboard_shortcut("Enter");
}
if let Some(owner) = self.self_id.get() {
self.page_tree.borrow().emit(owner, builder);
}
}
fn children(&self) -> Vec<WidgetId> {
self.root_child_id.into_iter().collect()
}
fn as_any_mut(&mut self) -> Option<&mut dyn std::any::Any> {
Some(self)
}
}
impl Drop for WebView {
fn drop(&mut self) {
if let Some(registry) = self.registry.borrow().as_ref() {
registry.unregister(self.web_view_id);
}
}
}
#[derive(Debug)]
struct EmptyOverlayContent;
impl Widget for EmptyOverlayContent {
fn layout_response(&self, proposal: SizeProposal, _ctx: &LayoutContext) -> LayoutResponse {
proposal.resolve(0.0, 0.0).into()
}
fn accessibility(&self, builder: &mut AccessNodeBuilder) {
builder.set_hidden();
}
}
fn intersect(a: Rect, b: Rect) -> Option<Rect> {
let x = a.x.max(b.x);
let y = a.y.max(b.y);
let right = a.right().min(b.right());
let bottom = a.bottom().min(b.bottom());
if right > x && bottom > y {
Some(Rect::new(x, y, right - x, bottom - y))
} else {
None
}
}
fn usable_scale(scale: f32) -> f32 {
if scale.is_finite() && scale > 0.0 {
scale
} else {
1.0
}
}
fn forwarded_key(event: &teksilo_core::event::WidgetEvent) -> Option<WebViewInputEvent> {
use teksilo_core::event::{Key, WidgetEvent};
let escape = |key: &Key, modifiers: &teksilo_core::event::Modifiers| {
*key == Key::Tab && modifiers.ctrl()
};
match event {
WidgetEvent::KeyDown {
key,
modifiers,
text,
} if !escape(key, modifiers) => Some(WebViewInputEvent::Key {
key: *key,
modifiers: *modifiers,
pressed: true,
text: text.clone(),
}),
WidgetEvent::KeyUp { key, modifiers } if !escape(key, modifiers) => {
Some(WebViewInputEvent::Key {
key: *key,
modifiers: *modifiers,
pressed: false,
text: None,
})
}
WidgetEvent::ImeComposition { text, cursor } => Some(WebViewInputEvent::ImeComposition {
text: text.clone(),
cursor: cursor.clone(),
}),
WidgetEvent::ImeCommit { text } => {
Some(WebViewInputEvent::ImeCommit { text: text.clone() })
}
_ => None,
}
}
fn forward_surface_pointer(
handlers: teksilo_core::widget_builder::HandlerSet,
surface: Rc<SurfaceView>,
handle: SharedHandle,
input: WebViewInput,
) -> teksilo_core::widget_builder::HandlerSet {
use teksilo_core::event::{EventResponse, WidgetEvent};
use teksilo_core::pointer::touch_action::TouchAction;
use teksilo_tokens::PointerKind;
let native = input.is_native();
let answer = if native {
EventResponse::Handled
} else {
EventResponse::Ignored
};
let send = |handle: &SharedHandle, event: WebViewInputEvent| {
if let Some(h) = handle.borrow().as_ref() {
h.send_input(&event);
}
};
let mut handlers = handlers;
if native {
handlers = handlers.touch_action(TouchAction::NONE).no_hit_slop();
}
let (pointer_surface, pointer_handle) = (surface.clone(), handle.clone());
handlers = handlers.on_pointer_event(move |event, ctx| {
let (position, pointer, button, phase) = match event {
WidgetEvent::PointerDown {
position,
pointer,
button,
..
} => (*position, *pointer, Some(*button), Some(true)),
WidgetEvent::PointerUp {
position,
pointer,
button,
..
} => (*position, *pointer, Some(*button), Some(false)),
WidgetEvent::PointerMove {
position, pointer, ..
} => (*position, *pointer, None, None),
_ => return EventResponse::Ignored,
};
let Some(page) = pointer_surface.page_point(position) else {
return answer;
};
let contact_kind = matches!(pointer.kind, PointerKind::Touch | PointerKind::Pen(_));
let pen = matches!(pointer.kind, PointerKind::Pen(_));
let mut contacts = pointer_surface.contacts.borrow_mut();
let in_contact = contacts.contains(&pointer.id);
let forwarded = match phase {
Some(true) if contact_kind => {
contacts.push(pointer.id);
WebViewInputEvent::Touch {
contact: pointer.id,
phase: WebViewTouchPhase::Down,
position: page,
pen,
}
}
Some(false) if in_contact => {
contacts.retain(|c| *c != pointer.id);
WebViewInputEvent::Touch {
contact: pointer.id,
phase: WebViewTouchPhase::Up,
position: page,
pen,
}
}
None if in_contact => WebViewInputEvent::Touch {
contact: pointer.id,
phase: WebViewTouchPhase::Move,
position: page,
pen,
},
Some(pressed) => {
if pressed && native {
ctx.capture_pointer();
}
WebViewInputEvent::PointerButton {
position: page,
button: button.unwrap_or(teksilo_core::event::PointerButton::Primary),
pressed,
}
}
None => WebViewInputEvent::PointerMove { position: page },
};
drop(contacts);
pointer_surface.last_pointer.set(Some(page));
if let Some(cursor) = pointer_surface.cursor.get() {
ctx.set_cursor(cursor);
}
send(&pointer_handle, forwarded);
answer
});
let (cancel_surface, cancel_handle) = (surface.clone(), handle.clone());
handlers = handlers.on_pointer_cancel(move |pointer, _reason, _ctx| {
let mut contacts = cancel_surface.contacts.borrow_mut();
if !contacts.contains(&pointer.id) {
return;
}
contacts.retain(|c| *c != pointer.id);
drop(contacts);
send(
&cancel_handle,
WebViewInputEvent::Touch {
contact: pointer.id,
phase: WebViewTouchPhase::Cancel,
position: cancel_surface.last_pointer.get().unwrap_or(Point::ZERO),
pen: matches!(pointer.kind, PointerKind::Pen(_)),
},
);
});
let leave_handle = handle.clone();
handlers = handlers.on_hover(move |entered, _ctx| {
if !entered {
send(&leave_handle, WebViewInputEvent::PointerLeave);
}
});
handlers.on_scroll(move |event, ctx| {
let WidgetEvent::Scroll {
delta,
modifiers,
window_position,
..
} = event
else {
return EventResponse::Ignored;
};
let position = window_position
.and_then(|p| ctx.to_local(p))
.and_then(|local| surface.page_point(local))
.or(surface.last_pointer.get());
let Some(position) = position else {
return answer;
};
send(
&handle,
WebViewInputEvent::Scroll {
position,
delta: *delta,
modifiers: *modifiers,
},
);
answer
})
}
fn apply_visibility(
handle: &SharedHandle,
visibility: &Rc<Cell<EngineVisibility>>,
applied: &Rc<Cell<bool>>,
) {
let want = visibility.get().resolved();
if applied.get() == want {
return;
}
if let Some(h) = handle.borrow().as_ref() {
h.set_visible(want);
applied.set(want);
}
}