ul_next/
overlay.rs

1//! Web-content overlay. Displays a web-page within an area of the main window.
2use std::sync::Arc;
3
4use crate::{view::View, Library};
5
6/// Web-content overlay. Displays a web-page within an area of the main window.
7///
8/// Each `Overlay` is essentially a View and an on-screen quad. You should
9/// create the Overlay then load content into the underlying View.
10///
11/// Can be created with [`Window::create_overlay`](crate::window::Window::create_overlay)
12/// or [`Window::create_overlay_with_view`](crate::window::Window::create_overlay_with_view).
13pub struct Overlay {
14    lib: Arc<Library>,
15    internal: ul_sys::ULOverlay,
16
17    view: View,
18}
19
20impl Overlay {
21    /// Internal function helper to create an overlay.
22    /// (See [`Window::create_overlay`](crate::window::Window::create_overlay))
23    pub(crate) unsafe fn create(
24        lib: Arc<Library>,
25        window: ul_sys::ULWindow,
26        width: u32,
27        height: u32,
28        x: i32,
29        y: i32,
30    ) -> Option<Self> {
31        let internal_overlay = lib.appcore().ulCreateOverlay(window, width, height, x, y);
32
33        if internal_overlay.is_null() {
34            return None;
35        }
36
37        let raw_view = lib.appcore().ulOverlayGetView(internal_overlay);
38        // the overlay owns the view, we can't need to destroy it on drop
39        let view = View::from_raw(lib.clone(), raw_view)?;
40        Some(Self {
41            lib,
42            internal: internal_overlay,
43            view,
44        })
45    }
46
47    /// Internal function helper to create an overlay with a view
48    /// (See [`Window::create_overlay_with_view`](crate::window::Window::create_overlay_with_view))
49    pub(crate) unsafe fn create_with_view(
50        lib: Arc<Library>,
51        window_raw: ul_sys::ULWindow,
52        view: View,
53        x: i32,
54        y: i32,
55    ) -> Option<Self> {
56        let internal = lib
57            .appcore()
58            .ulCreateOverlayWithView(window_raw, view.to_ul(), x, y);
59        if internal.is_null() {
60            return None;
61        }
62
63        Some(Self {
64            lib,
65            internal,
66            view,
67        })
68    }
69}
70
71impl Overlay {
72    /// Get the underlying View.
73    pub fn view(&self) -> &View {
74        &self.view
75    }
76
77    /// Get the width (in pixels).
78    pub fn width(&self) -> u32 {
79        unsafe { self.lib.appcore().ulOverlayGetWidth(self.internal) }
80    }
81
82    /// Get the height (in pixels).
83    pub fn height(&self) -> u32 {
84        unsafe { self.lib.appcore().ulOverlayGetHeight(self.internal) }
85    }
86
87    /// Get the x-position (offset from the left of the Window), in pixels.
88    pub fn x(&self) -> i32 {
89        unsafe { self.lib.appcore().ulOverlayGetX(self.internal) }
90    }
91
92    /// Get the y-position (offset from the top of the Window), in pixels.
93    pub fn y(&self) -> i32 {
94        unsafe { self.lib.appcore().ulOverlayGetY(self.internal) }
95    }
96
97    /// Whether or not the overlay is hidden (not drawn).
98    pub fn is_hidden(&self) -> bool {
99        unsafe { self.lib.appcore().ulOverlayIsHidden(self.internal) }
100    }
101
102    /// Show the overlay.
103    pub fn show(&self) {
104        unsafe { self.lib.appcore().ulOverlayShow(self.internal) }
105    }
106
107    /// Hide the overlay (will no longer be drawn)
108    pub fn hide(&self) {
109        unsafe { self.lib.appcore().ulOverlayHide(self.internal) }
110    }
111
112    /// Whether or not this overlay has keyboard focus.
113    pub fn has_focus(&self) -> bool {
114        unsafe { self.lib.appcore().ulOverlayHasFocus(self.internal) }
115    }
116
117    /// Grant this overlay exclusive keyboard focus.
118    pub fn focus(&self) {
119        unsafe { self.lib.appcore().ulOverlayFocus(self.internal) }
120    }
121
122    /// Remove keyboard focus.
123    pub fn unfocus(&self) {
124        unsafe { self.lib.appcore().ulOverlayUnfocus(self.internal) }
125    }
126
127    /// Move the overlay to a new position (in pixels).
128    pub fn move_to(&self, x: i32, y: i32) {
129        unsafe { self.lib.appcore().ulOverlayMoveTo(self.internal, x, y) }
130    }
131
132    /// Resize the overlay (and underlying View), dimensions should be
133    /// specified in pixels.
134    pub fn resize(&self, width: u32, height: u32) {
135        unsafe {
136            self.lib
137                .appcore()
138                .ulOverlayResize(self.internal, width, height)
139        }
140    }
141
142    // only found in C++ and not in the C API yet.
143    // pub fn need_repaint(&self) -> bool {
144    // }
145}
146
147impl Drop for Overlay {
148    fn drop(&mut self) {
149        unsafe {
150            self.lib.appcore().ulDestroyOverlay(self.internal);
151        }
152    }
153}