| | | 1 | | // SPDX-FileCopyrightText: 2026 Alper Çelik |
| | | 2 | | // SPDX-License-Identifier: MIT OR Apache-2.0 |
| | | 3 | | |
| | | 4 | | //! Top-level window registry for the Qt runtime. |
| | | 5 | | //! |
| | | 6 | | //! Owns one `QWidget` per Tauri window, keyed by the stable handle handed to |
| | | 7 | | //! Rust. All entry points run on the Qt GUI thread (the runtime dispatchers |
| | | 8 | | //! call them from the thread that owns the window) and each one tolerates an |
| | | 9 | | //! unknown handle. |
| | | 10 | | |
| | | 11 | | #include "qt_window.hpp" |
| | | 12 | | |
| | | 13 | | // cxx-generated header of the bridge in `src/qt.rs`; declares the |
| | | 14 | | // `qt_on_window_event` callback with the linkage cxx expects, so it must not |
| | | 15 | | // be hand-declared here. |
| | | 16 | | #include "tauri-runtime-qt/src/qt.rs.h" |
| | | 17 | | #include "tauri-runtime-qt/src/bridge/window.rs.h" |
| | | 18 | | |
| | | 19 | | #include <QtCore/QCoreApplication> |
| | | 20 | | #include <QtCore/QPointer> |
| | | 21 | | #include <QtCore/QSize> |
| | | 22 | | #include <QtCore/QString> |
| | | 23 | | #include <QtCore/qnumeric.h> |
| | | 24 | | #include <QtGui/QColor> |
| | | 25 | | #include <QtGui/QCloseEvent> |
| | | 26 | | #include <QtGui/QGuiApplication> |
| | | 27 | | #include <QtGui/QIcon> |
| | | 28 | | #include <QtGui/QImage> |
| | | 29 | | #include <QtGui/QMoveEvent> |
| | | 30 | | #include <QtGui/QPalette> |
| | | 31 | | #include <QtGui/QPixmap> |
| | | 32 | | #include <QtGui/QResizeEvent> |
| | | 33 | | #include <QtGui/QScreen> |
| | | 34 | | #include <QtGui/QStyleHints> |
| | | 35 | | #include <QtGui/QWindow> |
| | | 36 | | #include <QtWidgets/QApplication> |
| | | 37 | | #include <QtWidgets/QLayout> |
| | | 38 | | #include <QtWidgets/QStyle> |
| | | 39 | | #include <QtWidgets/QVBoxLayout> |
| | | 40 | | #include <QtWidgets/QWidget> |
| | | 41 | | |
| | | 42 | | #include <cstddef> |
| | | 43 | | #include <cstdint> |
| | | 44 | | #include <string> |
| | | 45 | | #include <unordered_map> |
| | | 46 | | |
| | | 47 | | namespace { |
| | | 48 | | |
| | | 49 | | // Event kinds of `qt_on_window_event` (see qt_window.hpp / src/qt.rs). |
| | | 50 | | constexpr std::int32_t kEventCloseRequested = 0; |
| | | 51 | | constexpr std::int32_t kEventFocused = 1; |
| | | 52 | | constexpr std::int32_t kEventUnfocused = 2; |
| | | 53 | | constexpr std::int32_t kEventResized = 3; |
| | | 54 | | constexpr std::int32_t kEventMoved = 4; |
| | | 55 | | |
| | | 56 | | /// Top-level window widget. Reports the window events tauri-runtime's |
| | | 57 | | /// `on_window_event` needs; it deliberately has no `Q_OBJECT` (build.rs does |
| | | 58 | | /// not run moc, so no meta-object code may be required by this class). |
| | | 59 | | class TauriWindow final : public QWidget { |
| | | 60 | | public: |
| | | 61 | | TauriWindow(std::uint64_t id, const QString& label) |
| | 21 | 62 | | : QWidget(nullptr, Qt::Window), m_id(id) { |
| | 21 | 63 | | setObjectName(label); |
| | 21 | 64 | | } |
| | | 65 | | |
| | | 66 | | /// Connects the QWindow-level notifications. Must run on the GUI thread. |
| | | 67 | | /// The platform QWindow is recreated when the window flags change (e.g. |
| | | 68 | | /// `qt_window_set_decorations`), so this re-connects whenever the native |
| | | 69 | | /// handle differs from the one we are attached to; safe to call often. |
| | 21 | 70 | | void attach() { |
| | 21 | 71 | | QWindow* handle = windowHandle(); |
| | 21 | 72 | | if (handle == nullptr) { |
| | 0 | 73 | | winId(); // force the native window so windowHandle() is valid |
| | 0 | 74 | | handle = windowHandle(); |
| | 0 | 75 | | if (handle == nullptr) { |
| | 0 | 76 | | return; // still not native: retry on the next attach() |
| | 0 | 77 | | } |
| | 0 | 78 | | } |
| | 21 | 79 | | if (handle == m_attached_handle) { |
| | 0 | 80 | | return; // already connected to this platform window |
| | 0 | 81 | | } |
| | 21 | 82 | | m_attached_handle = handle; // QPointer: nulls out if the handle dies |
| | 21 | 83 | | m_ready = true; // the handle is known to Rust from here on |
| | | 84 | | // `QWindow::activeChanged` carries no argument (Qt 6): read the state. |
| | 21 | 85 | | QObject::connect(handle, &QWindow::activeChanged, this, [this]() { |
| | 0 | 86 | | if (QWindow* current = m_attached_handle) { |
| | 0 | 87 | | emit_event(current->isActive() ? kEventFocused : kEventUnfocused, std::string()); |
| | 0 | 88 | | } |
| | 0 | 89 | | }); |
| | 21 | 90 | | } |
| | | 91 | | |
| | | 92 | | /// Enters or leaves fullscreen. Leaving restores the maximized state the |
| | | 93 | | /// window had before it went fullscreen (`showNormal()` would drop it). |
| | | 94 | | /// The request is mirrored because a window manager confirms |
| | | 95 | | /// `Qt::WindowFullScreen` asynchronously — `isFullScreen()` can still lag |
| | | 96 | | /// behind — while `qt_window_is_fullscreen` keeps reporting Qt's view. |
| | | 97 | | /// The state of a hidden window is applied when it is next shown. |
| | 42 | 98 | | void set_fullscreen(bool fullscreen) { |
| | 42 | 99 | | if (fullscreen) { |
| | 21 | 100 | | if (m_fullscreen_requested && isFullScreen()) { |
| | 0 | 101 | | return; |
| | 0 | 102 | | } |
| | 21 | 103 | | m_pre_fullscreen_state = windowState() & (Qt::WindowMinimized | Qt::WindowMaximized); |
| | 21 | 104 | | m_fullscreen_requested = true; |
| | 21 | 105 | | setWindowState(windowState() | Qt::WindowFullScreen); |
| | 21 | 106 | | } else { |
| | 21 | 107 | | if (!m_fullscreen_requested && !isFullScreen()) { |
| | 0 | 108 | | return; |
| | 0 | 109 | | } |
| | 21 | 110 | | m_fullscreen_requested = false; |
| | 21 | 111 | | setWindowState((windowState() & ~Qt::WindowFullScreen) | m_pre_fullscreen_state); |
| | 21 | 112 | | } |
| | 42 | 113 | | if (isVisible()) { |
| | 42 | 114 | | show(); // apply the new state immediately |
| | 42 | 115 | | } |
| | 42 | 116 | | } |
| | | 117 | | |
| | | 118 | | /// Qt exposes neither a resizable getter nor a setter; the size hints are |
| | | 119 | | /// what a window manager honours, so disabling resizing pins the window to |
| | | 120 | | /// its current size (`minimumSize == maximumSize`) and remembers the |
| | | 121 | | /// constraints in force so that re-enabling restores them. |
| | 0 | 122 | | void set_resizable(bool resizable) { |
| | 0 | 123 | | if (resizable) { |
| | 0 | 124 | | if (!m_pinned_by_resizable) { |
| | 0 | 125 | | return; |
| | 0 | 126 | | } |
| | 0 | 127 | | setMinimumSize(m_pinned_min); |
| | 0 | 128 | | setMaximumSize(m_pinned_max); |
| | 0 | 129 | | m_pinned_by_resizable = false; |
| | 0 | 130 | | } else { |
| | 0 | 131 | | if (m_pinned_by_resizable) { |
| | 0 | 132 | | return; |
| | 0 | 133 | | } |
| | 0 | 134 | | m_pinned_min = minimumSize(); |
| | 0 | 135 | | m_pinned_max = maximumSize(); |
| | 0 | 136 | | m_pinned_by_resizable = true; |
| | 0 | 137 | | setMinimumSize(size()); |
| | 0 | 138 | | setMaximumSize(size()); |
| | 0 | 139 | | } |
| | 0 | 140 | | } |
| | | 141 | | |
| | | 142 | | /// A window is resizable exactly while its size hints do not pin both |
| | | 143 | | /// dimensions to one value (`set_resizable(false)` installs |
| | | 144 | | /// `minimumSize == maximumSize`; explicit equal constraints pin it too). |
| | 42 | 145 | | bool is_resizable() const { |
| | 42 | 146 | | return minimumSize() != maximumSize(); |
| | 42 | 147 | | } |
| | | 148 | | |
| | | 149 | | /// Sets the minimum size from logical pixels. While resizing is disabled |
| | | 150 | | /// the remembered pre-pin constraint is kept coherent, so that |
| | | 151 | | /// `set_resizable(true)` restores what the caller last asked for. |
| | 0 | 152 | | void set_min_size(double width, double height) { |
| | 0 | 153 | | const QSize size(qRound(width), qRound(height)); |
| | 0 | 154 | | if (m_pinned_by_resizable) { |
| | 0 | 155 | | m_pinned_min = size; |
| | 0 | 156 | | } |
| | 0 | 157 | | setMinimumSize(size); |
| | 0 | 158 | | } |
| | | 159 | | |
| | | 160 | | /// Sets the maximum size from logical pixels. |
| | 0 | 161 | | void set_max_size(double width, double height) { |
| | 0 | 162 | | const QSize size(qRound(width), qRound(height)); |
| | 0 | 163 | | if (m_pinned_by_resizable) { |
| | 0 | 164 | | m_pinned_max = size; |
| | 0 | 165 | | } |
| | 0 | 166 | | setMaximumSize(size); |
| | 0 | 167 | | } |
| | | 168 | | |
| | | 169 | | protected: |
| | 0 | 170 | | void closeEvent(QCloseEvent* event) override { |
| | | 171 | | // The Rust side observes the request and may re-issue `close` after |
| | | 172 | | // cleaning up; the callback is void, so the default handler runs. |
| | 0 | 173 | | emit_event(kEventCloseRequested, std::string()); |
| | 0 | 174 | | QWidget::closeEvent(event); |
| | 0 | 175 | | } |
| | | 176 | | |
| | 21 | 177 | | void resizeEvent(QResizeEvent* event) override { |
| | 21 | 178 | | QWidget::resizeEvent(event); |
| | 21 | 179 | | emit_event(kEventResized, std::to_string(event->size().width()) + "x" + |
| | 21 | 180 | | std::to_string(event->size().height())); |
| | 21 | 181 | | } |
| | | 182 | | |
| | 21 | 183 | | void moveEvent(QMoveEvent* event) override { |
| | 21 | 184 | | QWidget::moveEvent(event); |
| | 21 | 185 | | emit_event(kEventMoved, std::to_string(event->pos().x()) + "," + |
| | 21 | 186 | | std::to_string(event->pos().y())); |
| | 21 | 187 | | } |
| | | 188 | | |
| | | 189 | | private: |
| | 42 | 190 | | void emit_event(std::int32_t kind, const std::string& payload) { |
| | 42 | 191 | | if (!m_ready) { |
| | 42 | 192 | | return; // handle not returned to Rust yet |
| | 42 | 193 | | } |
| | 0 | 194 | | qt_on_window_event(m_id, kind, rust::Str(payload)); |
| | 0 | 195 | | } |
| | | 196 | | |
| | | 197 | | std::uint64_t m_id; |
| | | 198 | | QPointer<QWindow> m_attached_handle; |
| | | 199 | | bool m_ready = false; |
| | | 200 | | /// Window state to restore when leaving fullscreen. |
| | | 201 | | Qt::WindowStates m_pre_fullscreen_state = Qt::WindowNoState; |
| | | 202 | | /// The last fullscreen state requested (the window manager confirms the |
| | | 203 | | /// change asynchronously, so `windowState()` alone is not enough). |
| | | 204 | | bool m_fullscreen_requested = false; |
| | | 205 | | /// Size constraints in force before `set_resizable(false)` pinned them. |
| | | 206 | | QSize m_pinned_min{0, 0}; |
| | | 207 | | QSize m_pinned_max{QWIDGETSIZE_MAX, QWIDGETSIZE_MAX}; |
| | | 208 | | bool m_pinned_by_resizable = false; |
| | | 209 | | }; |
| | | 210 | | |
| | 2835 | 211 | | std::unordered_map<std::uint64_t, TauriWindow*>& registry() { |
| | 2835 | 212 | | static std::unordered_map<std::uint64_t, TauriWindow*> windows; |
| | 2835 | 213 | | return windows; |
| | 2835 | 214 | | } |
| | | 215 | | |
| | | 216 | | std::uint64_t next_handle = 0; |
| | | 217 | | |
| | | 218 | | /// Lookup that never throws and never returns a dangling widget. |
| | 1407 | 219 | | TauriWindow* find_window(std::uint64_t id) { |
| | 1407 | 220 | | if (id == 0) { |
| | 0 | 221 | | return nullptr; |
| | 0 | 222 | | } |
| | 1407 | 223 | | const auto it = registry().find(id); |
| | 1407 | 224 | | return it == registry().end() ? nullptr : it->second; |
| | 1407 | 225 | | } |
| | | 226 | | |
| | | 227 | | /// Applies new window flags, restoring visibility: changing a window's flags |
| | | 228 | | /// re-parents the widget (hiding it) and may replace the platform `QWindow`, |
| | | 229 | | /// so the event connections are re-established as well. |
| | 0 | 230 | | void apply_window_flags(TauriWindow* window, Qt::WindowFlags flags) { |
| | 0 | 231 | | if (flags == window->windowFlags()) { |
| | 0 | 232 | | return; // a no-op flag change would needlessly hide and re-show it |
| | 0 | 233 | | } |
| | 0 | 234 | | const bool was_visible = window->isVisible(); |
| | 0 | 235 | | window->setWindowFlags(flags); |
| | 0 | 236 | | if (was_visible) { |
| | 0 | 237 | | window->show(); |
| | 0 | 238 | | } |
| | 0 | 239 | | window->attach(); |
| | 0 | 240 | | } |
| | | 241 | | |
| | | 242 | | /// tauri's `ResizeDirection` numbering (as mapped by `lib.rs`) to Qt edges. |
| | 0 | 243 | | Qt::Edges edges_for_direction(std::int32_t direction) { |
| | 0 | 244 | | switch (direction) { |
| | 0 | 245 | | case 1: // East |
| | 0 | 246 | | return Qt::RightEdge; |
| | 0 | 247 | | case 2: // North |
| | 0 | 248 | | return Qt::TopEdge; |
| | 0 | 249 | | case 3: // NorthEast |
| | 0 | 250 | | return Qt::TopEdge | Qt::RightEdge; |
| | 0 | 251 | | case 4: // NorthWest |
| | 0 | 252 | | return Qt::TopEdge | Qt::LeftEdge; |
| | 0 | 253 | | case 5: // South |
| | 0 | 254 | | return Qt::BottomEdge; |
| | 0 | 255 | | case 6: // SouthEast |
| | 0 | 256 | | return Qt::BottomEdge | Qt::RightEdge; |
| | 0 | 257 | | case 7: // SouthWest |
| | 0 | 258 | | return Qt::BottomEdge | Qt::LeftEdge; |
| | 0 | 259 | | case 8: // West |
| | 0 | 260 | | return Qt::LeftEdge; |
| | 0 | 261 | | default: |
| | 0 | 262 | | return Qt::Edges(); // unknown: no direction |
| | 0 | 263 | | } |
| | 0 | 264 | | } |
| | | 265 | | |
| | | 266 | | /// UTF-8 view of a QString for the Rust side. |
| | 42 | 267 | | rust::String to_rust_string(const QString& text) { |
| | 42 | 268 | | const QByteArray utf8 = text.toUtf8(); |
| | 42 | 269 | | return rust::String(utf8.constData(), static_cast<std::size_t>(utf8.size())); |
| | 42 | 270 | | } |
| | | 271 | | |
| | | 272 | | } // namespace |
| | | 273 | | |
| | | 274 | | uint64_t qt_window_create(rust::Str label, rust::Str title, int width, int height, bool visible, |
| | 21 | 275 | | bool decorations) { |
| | 21 | 276 | | if (QCoreApplication::instance() == nullptr) { |
| | 0 | 277 | | return 0; // a QWidget must not be constructed before the QApplication |
| | 0 | 278 | | } |
| | | 279 | | |
| | 21 | 280 | | const std::uint64_t id = ++next_handle; |
| | 21 | 281 | | auto* window = new TauriWindow(id, QString::fromUtf8(label.data(), static_cast<int>(label.size()))); |
| | 21 | 282 | | window->setWindowTitle(QString::fromUtf8(title.data(), static_cast<int>(title.size()))); |
| | 21 | 283 | | if (width > 0 && height > 0) { |
| | 21 | 284 | | window->resize(width, height); |
| | 21 | 285 | | } |
| | 21 | 286 | | if (!decorations) { |
| | 0 | 287 | | window->setWindowFlag(Qt::FramelessWindowHint, true); |
| | 0 | 288 | | } |
| | | 289 | | |
| | 21 | 290 | | registry().emplace(id, window); |
| | | 291 | | |
| | 21 | 292 | | if (visible) { |
| | 21 | 293 | | window->show(); |
| | 21 | 294 | | } |
| | 21 | 295 | | window->attach(); |
| | 21 | 296 | | return id; |
| | 21 | 297 | | } |
| | | 298 | | |
| | 0 | 299 | | void qt_window_show(uint64_t id) { |
| | 0 | 300 | | if (TauriWindow* window = find_window(id)) { |
| | 0 | 301 | | window->show(); // focus/raise are separate tauri calls (set_focus) |
| | 0 | 302 | | window->attach(); |
| | 0 | 303 | | } |
| | 0 | 304 | | } |
| | | 305 | | |
| | 0 | 306 | | void qt_window_hide(uint64_t id) { |
| | 0 | 307 | | if (TauriWindow* window = find_window(id)) { |
| | 0 | 308 | | window->hide(); |
| | 0 | 309 | | } |
| | 0 | 310 | | } |
| | | 311 | | |
| | 0 | 312 | | void qt_window_close(uint64_t id) { |
| | 0 | 313 | | if (TauriWindow* window = find_window(id)) { |
| | 0 | 314 | | window->close(); // delivers closeEvent -> kind 0, then hides |
| | 0 | 315 | | } |
| | 0 | 316 | | } |
| | | 317 | | |
| | 21 | 318 | | void qt_window_set_title(uint64_t id, rust::Str title) { |
| | 21 | 319 | | if (TauriWindow* window = find_window(id)) { |
| | 21 | 320 | | window->setWindowTitle(QString::fromUtf8(title.data(), static_cast<int>(title.size()))); |
| | 21 | 321 | | } |
| | 21 | 322 | | } |
| | | 323 | | |
| | 0 | 324 | | void qt_window_set_size(uint64_t id, int width, int height) { |
| | 0 | 325 | | if (TauriWindow* window = find_window(id); window != nullptr && width > 0 && height > 0) { |
| | 0 | 326 | | window->resize(width, height); |
| | 0 | 327 | | } |
| | 0 | 328 | | } |
| | | 329 | | |
| | 0 | 330 | | void qt_window_center(uint64_t id) { |
| | 0 | 331 | | TauriWindow* window = find_window(id); |
| | 0 | 332 | | if (window == nullptr) { |
| | 0 | 333 | | return; |
| | 0 | 334 | | } |
| | | 335 | | |
| | 0 | 336 | | QScreen* screen = nullptr; |
| | 0 | 337 | | if (QWindow* handle = window->windowHandle()) { |
| | 0 | 338 | | screen = handle->screen(); |
| | 0 | 339 | | } |
| | 0 | 340 | | if (screen == nullptr) { |
| | 0 | 341 | | screen = QGuiApplication::screenAt(window->frameGeometry().center()); |
| | 0 | 342 | | } |
| | 0 | 343 | | if (screen == nullptr) { |
| | 0 | 344 | | screen = QGuiApplication::primaryScreen(); |
| | 0 | 345 | | } |
| | 0 | 346 | | if (screen == nullptr) { |
| | 0 | 347 | | return; |
| | 0 | 348 | | } |
| | | 349 | | |
| | 0 | 350 | | const QRect target = QStyle::alignedRect(Qt::LeftToRight, Qt::AlignCenter, window->size(), |
| | 0 | 351 | | screen->availableGeometry()); |
| | 0 | 352 | | window->move(target.topLeft()); |
| | 0 | 353 | | } |
| | | 354 | | |
| | 0 | 355 | | void qt_window_set_decorations(uint64_t id, bool decorations) { |
| | 0 | 356 | | TauriWindow* window = find_window(id); |
| | 0 | 357 | | if (window == nullptr) { |
| | 0 | 358 | | return; |
| | 0 | 359 | | } |
| | | 360 | | |
| | 0 | 361 | | Qt::WindowFlags flags = window->windowFlags(); |
| | 0 | 362 | | flags.setFlag(Qt::FramelessWindowHint, !decorations); |
| | 0 | 363 | | apply_window_flags(window, flags); |
| | 0 | 364 | | } |
| | | 365 | | |
| | 0 | 366 | | void qt_window_set_maximized(uint64_t id, bool maximized) { |
| | 0 | 367 | | TauriWindow* window = find_window(id); |
| | 0 | 368 | | if (window == nullptr) { |
| | 0 | 369 | | return; |
| | 0 | 370 | | } |
| | | 371 | | |
| | 0 | 372 | | const Qt::WindowStates state = window->windowState(); |
| | 0 | 373 | | window->setWindowState(maximized ? (state | Qt::WindowMaximized) : (state & ~Qt::WindowMaximized)); |
| | 0 | 374 | | } |
| | | 375 | | |
| | 42 | 376 | | bool qt_window_is_visible(uint64_t id) { |
| | 42 | 377 | | TauriWindow* window = find_window(id); |
| | 42 | 378 | | return window != nullptr && window->isVisible(); |
| | 42 | 379 | | } |
| | | 380 | | |
| | 210 | 381 | | double qt_window_scale_factor(uint64_t id) { |
| | 210 | 382 | | TauriWindow* window = find_window(id); |
| | 210 | 383 | | if (window == nullptr) { |
| | 0 | 384 | | return 1.0; |
| | 0 | 385 | | } |
| | 210 | 386 | | if (QWindow* handle = window->windowHandle()) { |
| | 210 | 387 | | return handle->devicePixelRatio(); // correct under Wayland fractional scaling |
| | 210 | 388 | | } |
| | 0 | 389 | | return window->devicePixelRatioF(); |
| | 210 | 390 | | } |
| | | 391 | | |
| | 21 | 392 | | uint64_t qt_window_winid(uint64_t id) { |
| | 21 | 393 | | TauriWindow* window = find_window(id); |
| | 21 | 394 | | if (window == nullptr) { |
| | 0 | 395 | | return 0; |
| | 0 | 396 | | } |
| | 21 | 397 | | return static_cast<std::uint64_t>(window->winId()); |
| | 21 | 398 | | } |
| | | 399 | | |
| | 21 | 400 | | void qt_window_attach_webview(uint64_t window_id, uint64_t webview_native_id) { |
| | 21 | 401 | | TauriWindow* window = find_window(window_id); |
| | 21 | 402 | | if (window == nullptr || webview_native_id == 0) { |
| | 0 | 403 | | return; |
| | 0 | 404 | | } |
| | | 405 | | |
| | | 406 | | // The webview agent hands us its QWebEngineView as an opaque pointer; the |
| | | 407 | | // widget fills the window's client area, so it is added to a zero-margin |
| | | 408 | | // layout that keeps it in sync with every resize. |
| | 21 | 409 | | auto* view = reinterpret_cast<QWidget*>(static_cast<quintptr>(webview_native_id)); |
| | 21 | 410 | | if (view == nullptr) { |
| | 0 | 411 | | return; |
| | 0 | 412 | | } |
| | | 413 | | |
| | 21 | 414 | | view->setParent(window); |
| | 21 | 415 | | QLayout* layout = window->layout(); |
| | 21 | 416 | | if (layout == nullptr) { |
| | 21 | 417 | | auto* box = new QVBoxLayout(window); |
| | 21 | 418 | | box->setContentsMargins(0, 0, 0, 0); |
| | 21 | 419 | | box->setSpacing(0); |
| | 21 | 420 | | layout = box; |
| | 21 | 421 | | } |
| | 21 | 422 | | layout->addWidget(view); |
| | 21 | 423 | | view->show(); |
| | 21 | 424 | | } |
| | | 425 | | |
| | | 426 | | // ---- window worker: registry and platform ---- |
| | | 427 | | |
| | 546 | 428 | | bool qt_window_alive(uint64_t id) { |
| | 546 | 429 | | return find_window(id) != nullptr; |
| | 546 | 430 | | } |
| | | 431 | | |
| | 0 | 432 | | rust::String qt_window_platform() { |
| | 0 | 433 | | return to_rust_string(QGuiApplication::platformName()); |
| | 0 | 434 | | } |
| | | 435 | | |
| | | 436 | | // ---- geometry (Qt device-independent pixels) ---- |
| | | 437 | | |
| | 42 | 438 | | QtWindowSize qt_window_inner_size(uint64_t id) { |
| | 42 | 439 | | TauriWindow* window = find_window(id); |
| | 42 | 440 | | if (window == nullptr) { |
| | 0 | 441 | | return {0.0, 0.0}; |
| | 0 | 442 | | } |
| | 42 | 443 | | const QSize size = window->size(); // client area, excluding frame |
| | 42 | 444 | | return {static_cast<double>(size.width()), static_cast<double>(size.height())}; |
| | 42 | 445 | | } |
| | | 446 | | |
| | 42 | 447 | | QtWindowSize qt_window_outer_size(uint64_t id) { |
| | 42 | 448 | | TauriWindow* window = find_window(id); |
| | 42 | 449 | | if (window == nullptr) { |
| | 0 | 450 | | return {0.0, 0.0}; |
| | 0 | 451 | | } |
| | 42 | 452 | | const QSize size = window->frameGeometry().size(); |
| | 42 | 453 | | return {static_cast<double>(size.width()), static_cast<double>(size.height())}; |
| | 42 | 454 | | } |
| | | 455 | | |
| | 42 | 456 | | QtWindowPoint qt_window_inner_position(uint64_t id) { |
| | 42 | 457 | | TauriWindow* window = find_window(id); |
| | 42 | 458 | | if (window == nullptr) { |
| | 0 | 459 | | return {0.0, 0.0}; |
| | 0 | 460 | | } |
| | 42 | 461 | | const QPoint origin = window->mapToGlobal(QPoint(0, 0)); |
| | 42 | 462 | | return {static_cast<double>(origin.x()), static_cast<double>(origin.y())}; |
| | 42 | 463 | | } |
| | | 464 | | |
| | 42 | 465 | | QtWindowPoint qt_window_outer_position(uint64_t id) { |
| | 42 | 466 | | TauriWindow* window = find_window(id); |
| | 42 | 467 | | if (window == nullptr) { |
| | 0 | 468 | | return {0.0, 0.0}; |
| | 0 | 469 | | } |
| | 42 | 470 | | const QPoint origin = window->frameGeometry().topLeft(); |
| | 42 | 471 | | return {static_cast<double>(origin.x()), static_cast<double>(origin.y())}; |
| | 42 | 472 | | } |
| | | 473 | | |
| | | 474 | | // ---- state ---- |
| | | 475 | | |
| | 42 | 476 | | rust::String qt_window_title(uint64_t id) { |
| | 42 | 477 | | TauriWindow* window = find_window(id); |
| | 42 | 478 | | if (window == nullptr) { |
| | 0 | 479 | | return rust::String(); |
| | 0 | 480 | | } |
| | 42 | 481 | | return to_rust_string(window->windowTitle()); |
| | 42 | 482 | | } |
| | | 483 | | |
| | 0 | 484 | | int32_t qt_window_theme(uint64_t id) { |
| | 0 | 485 | | if (find_window(id) == nullptr) { |
| | 0 | 486 | | return 0; |
| | 0 | 487 | | } |
| | | 488 | | // 6.5+: the platform's colour scheme; `Unknown` means it does not report one. |
| | 0 | 489 | | switch (QGuiApplication::styleHints()->colorScheme()) { |
| | 0 | 490 | | case Qt::ColorScheme::Light: |
| | 0 | 491 | | return 1; |
| | 0 | 492 | | case Qt::ColorScheme::Dark: |
| | 0 | 493 | | return 2; |
| | 0 | 494 | | case Qt::ColorScheme::Unknown: |
| | 0 | 495 | | default: |
| | 0 | 496 | | return 0; |
| | 0 | 497 | | } |
| | 0 | 498 | | } |
| | | 499 | | |
| | 42 | 500 | | bool qt_window_is_fullscreen(uint64_t id) { |
| | 42 | 501 | | TauriWindow* window = find_window(id); |
| | 42 | 502 | | return window != nullptr && window->isFullScreen(); |
| | 42 | 503 | | } |
| | | 504 | | |
| | 42 | 505 | | bool qt_window_is_minimized(uint64_t id) { |
| | 42 | 506 | | TauriWindow* window = find_window(id); |
| | 42 | 507 | | return window != nullptr && window->isMinimized(); |
| | 42 | 508 | | } |
| | | 509 | | |
| | 42 | 510 | | bool qt_window_is_maximized(uint64_t id) { |
| | 42 | 511 | | TauriWindow* window = find_window(id); |
| | 42 | 512 | | return window != nullptr && window->isMaximized(); |
| | 42 | 513 | | } |
| | | 514 | | |
| | 42 | 515 | | bool qt_window_is_focused(uint64_t id) { |
| | 42 | 516 | | TauriWindow* window = find_window(id); |
| | 42 | 517 | | return window != nullptr && window->isActiveWindow(); |
| | 42 | 518 | | } |
| | | 519 | | |
| | 42 | 520 | | bool qt_window_is_decorated(uint64_t id) { |
| | 42 | 521 | | TauriWindow* window = find_window(id); |
| | 42 | 522 | | return window != nullptr && !window->windowFlags().testFlag(Qt::FramelessWindowHint); |
| | 42 | 523 | | } |
| | | 524 | | |
| | 42 | 525 | | bool qt_window_is_resizable(uint64_t id) { |
| | 42 | 526 | | TauriWindow* window = find_window(id); |
| | 42 | 527 | | return window != nullptr && window->is_resizable(); |
| | 42 | 528 | | } |
| | | 529 | | |
| | 0 | 530 | | bool qt_window_is_enabled(uint64_t id) { |
| | 0 | 531 | | TauriWindow* window = find_window(id); |
| | 0 | 532 | | return window != nullptr && window->isEnabled(); |
| | 0 | 533 | | } |
| | | 534 | | |
| | 42 | 535 | | bool qt_window_is_always_on_top(uint64_t id) { |
| | 42 | 536 | | TauriWindow* window = find_window(id); |
| | | 537 | | // The hint Qt hands the window manager. Wayland compositors never honour |
| | | 538 | | // it, and `qt_window_set_always_on_top` refuses to set it there, so the |
| | | 539 | | // flag reports the truth on both platforms. |
| | 42 | 540 | | return window != nullptr && window->windowFlags().testFlag(Qt::WindowStaysOnTopHint); |
| | 42 | 541 | | } |
| | | 542 | | |
| | 0 | 543 | | bool qt_window_is_maximizable(uint64_t id) { |
| | 0 | 544 | | TauriWindow* window = find_window(id); |
| | 0 | 545 | | return window != nullptr && window->windowFlags().testFlag(Qt::WindowMaximizeButtonHint); |
| | 0 | 546 | | } |
| | | 547 | | |
| | 0 | 548 | | bool qt_window_is_minimizable(uint64_t id) { |
| | 0 | 549 | | TauriWindow* window = find_window(id); |
| | 0 | 550 | | return window != nullptr && window->windowFlags().testFlag(Qt::WindowMinimizeButtonHint); |
| | 0 | 551 | | } |
| | | 552 | | |
| | 0 | 553 | | bool qt_window_is_closable(uint64_t id) { |
| | 0 | 554 | | TauriWindow* window = find_window(id); |
| | 0 | 555 | | return window != nullptr && window->windowFlags().testFlag(Qt::WindowCloseButtonHint); |
| | 0 | 556 | | } |
| | | 557 | | |
| | | 558 | | // ---- setters ---- |
| | | 559 | | |
| | 42 | 560 | | void qt_window_set_fullscreen(uint64_t id, bool fullscreen) { |
| | 42 | 561 | | if (TauriWindow* window = find_window(id)) { |
| | 42 | 562 | | window->set_fullscreen(fullscreen); |
| | 42 | 563 | | } |
| | 42 | 564 | | } |
| | | 565 | | |
| | 0 | 566 | | void qt_window_minimize(uint64_t id) { |
| | 0 | 567 | | if (TauriWindow* window = find_window(id)) { |
| | 0 | 568 | | window->showMinimized(); |
| | 0 | 569 | | } |
| | 0 | 570 | | } |
| | | 571 | | |
| | 0 | 572 | | void qt_window_unminimize(uint64_t id) { |
| | 0 | 573 | | TauriWindow* window = find_window(id); |
| | 0 | 574 | | if (window == nullptr) { |
| | 0 | 575 | | return; |
| | 0 | 576 | | } |
| | 0 | 577 | | window->setWindowState(window->windowState() & ~Qt::WindowMinimized); |
| | | 578 | | // Best effort: a Wayland compositor decides whether activation happens. |
| | 0 | 579 | | window->activateWindow(); |
| | 0 | 580 | | } |
| | | 581 | | |
| | 0 | 582 | | void qt_window_set_min_size(uint64_t id, double width, double height) { |
| | 0 | 583 | | if (TauriWindow* window = find_window(id)) { |
| | 0 | 584 | | window->set_min_size(width, height); |
| | 0 | 585 | | } |
| | 0 | 586 | | } |
| | | 587 | | |
| | 0 | 588 | | void qt_window_set_max_size(uint64_t id, double width, double height) { |
| | 0 | 589 | | if (TauriWindow* window = find_window(id)) { |
| | 0 | 590 | | window->set_max_size(width, height); |
| | 0 | 591 | | } |
| | 0 | 592 | | } |
| | | 593 | | |
| | | 594 | | bool qt_window_set_icon(uint64_t id, rust::Slice<const uint8_t> rgba, uint32_t width, |
| | 0 | 595 | | uint32_t height) { |
| | 0 | 596 | | TauriWindow* window = find_window(id); |
| | 0 | 597 | | if (window == nullptr) { |
| | 0 | 598 | | return false; |
| | 0 | 599 | | } |
| | 0 | 600 | | const std::size_t expected = |
| | 0 | 601 | | static_cast<std::size_t>(width) * static_cast<std::size_t>(height) * 4u; |
| | 0 | 602 | | if (width == 0 || height == 0 || rgba.size() != expected) { |
| | 0 | 603 | | return false; |
| | 0 | 604 | | } |
| | | 605 | | // The QImage is a view onto the Rust buffer, so `QPixmap::fromImage` must |
| | | 606 | | // happen inside this call (it converts/copies into an owned pixmap). |
| | 0 | 607 | | const QImage image(rgba.data(), static_cast<int>(width), static_cast<int>(height), |
| | 0 | 608 | | QImage::Format_RGBA8888); |
| | 0 | 609 | | window->setWindowIcon(QIcon(QPixmap::fromImage(image))); |
| | 0 | 610 | | return true; |
| | 0 | 611 | | } |
| | | 612 | | |
| | 0 | 613 | | void qt_window_set_focus(uint64_t id) { |
| | 0 | 614 | | TauriWindow* window = find_window(id); |
| | 0 | 615 | | if (window == nullptr) { |
| | 0 | 616 | | return; |
| | 0 | 617 | | } |
| | | 618 | | // Both calls are activation requests: a Wayland compositor may refuse one |
| | | 619 | | // that did not follow user input, and Qt has no way to report that back. |
| | 0 | 620 | | window->activateWindow(); |
| | 0 | 621 | | window->raise(); |
| | 0 | 622 | | } |
| | | 623 | | |
| | 0 | 624 | | void qt_window_set_resizable(uint64_t id, bool resizable) { |
| | 0 | 625 | | if (TauriWindow* window = find_window(id)) { |
| | 0 | 626 | | window->set_resizable(resizable); |
| | 0 | 627 | | } |
| | 0 | 628 | | } |
| | | 629 | | |
| | 0 | 630 | | void qt_window_set_enabled(uint64_t id, bool enabled) { |
| | 0 | 631 | | if (TauriWindow* window = find_window(id)) { |
| | 0 | 632 | | window->setEnabled(enabled); |
| | 0 | 633 | | } |
| | 0 | 634 | | } |
| | | 635 | | |
| | 0 | 636 | | void qt_window_set_always_on_top(uint64_t id, bool on_top) { |
| | 0 | 637 | | TauriWindow* window = find_window(id); |
| | 0 | 638 | | if (window == nullptr) { |
| | 0 | 639 | | return; |
| | 0 | 640 | | } |
| | 0 | 641 | | Qt::WindowFlags flags = window->windowFlags(); |
| | 0 | 642 | | flags.setFlag(Qt::WindowStaysOnTopHint, on_top); |
| | 0 | 643 | | if (on_top) { |
| | | 644 | | // The two stacking hints contradict each other; keep them exclusive. |
| | 0 | 645 | | flags.setFlag(Qt::WindowStaysOnBottomHint, false); |
| | 0 | 646 | | } |
| | 0 | 647 | | apply_window_flags(window, flags); |
| | 0 | 648 | | } |
| | | 649 | | |
| | 0 | 650 | | void qt_window_set_always_on_bottom(uint64_t id, bool on_bottom) { |
| | 0 | 651 | | TauriWindow* window = find_window(id); |
| | 0 | 652 | | if (window == nullptr) { |
| | 0 | 653 | | return; |
| | 0 | 654 | | } |
| | 0 | 655 | | Qt::WindowFlags flags = window->windowFlags(); |
| | 0 | 656 | | flags.setFlag(Qt::WindowStaysOnBottomHint, on_bottom); |
| | 0 | 657 | | if (on_bottom) { |
| | 0 | 658 | | flags.setFlag(Qt::WindowStaysOnTopHint, false); |
| | 0 | 659 | | } |
| | 0 | 660 | | apply_window_flags(window, flags); |
| | 0 | 661 | | } |
| | | 662 | | |
| | 0 | 663 | | void qt_window_set_background_color(uint64_t id, uint8_t r, uint8_t g, uint8_t b, uint8_t a) { |
| | 0 | 664 | | TauriWindow* window = find_window(id); |
| | 0 | 665 | | if (window == nullptr) { |
| | 0 | 666 | | return; |
| | 0 | 667 | | } |
| | 0 | 668 | | QPalette palette = window->palette(); |
| | 0 | 669 | | palette.setColor(QPalette::Window, QColor(r, g, b, a)); |
| | 0 | 670 | | window->setPalette(palette); |
| | 0 | 671 | | window->setAutoFillBackground(true); |
| | 0 | 672 | | } |
| | | 673 | | |
| | 0 | 674 | | void qt_window_reset_background_color(uint64_t id) { |
| | 0 | 675 | | TauriWindow* window = find_window(id); |
| | 0 | 676 | | if (window == nullptr) { |
| | 0 | 677 | | return; |
| | 0 | 678 | | } |
| | 0 | 679 | | window->setPalette(QApplication::palette()); |
| | 0 | 680 | | window->setAutoFillBackground(false); |
| | 0 | 681 | | } |
| | | 682 | | |
| | 0 | 683 | | void qt_window_set_position(uint64_t id, int x, int y) { |
| | 0 | 684 | | if (TauriWindow* window = find_window(id)) { |
| | | 685 | | // For a top-level widget `move` is the frame position on the desktop. |
| | 0 | 686 | | window->move(x, y); |
| | 0 | 687 | | } |
| | 0 | 688 | | } |
| | | 689 | | |
| | 0 | 690 | | bool qt_window_start_dragging(uint64_t id) { |
| | 0 | 691 | | TauriWindow* window = find_window(id); |
| | 0 | 692 | | if (window == nullptr) { |
| | 0 | 693 | | return false; |
| | 0 | 694 | | } |
| | 0 | 695 | | QWindow* handle = window->windowHandle(); |
| | 0 | 696 | | if (handle == nullptr) { |
| | 0 | 697 | | return false; |
| | 0 | 698 | | } |
| | 0 | 699 | | return handle->startSystemMove(); |
| | 0 | 700 | | } |
| | | 701 | | |
| | 0 | 702 | | bool qt_window_start_resize_dragging(uint64_t id, int32_t direction) { |
| | 0 | 703 | | TauriWindow* window = find_window(id); |
| | 0 | 704 | | if (window == nullptr) { |
| | 0 | 705 | | return false; |
| | 0 | 706 | | } |
| | 0 | 707 | | QWindow* handle = window->windowHandle(); |
| | 0 | 708 | | if (handle == nullptr) { |
| | 0 | 709 | | return false; |
| | 0 | 710 | | } |
| | 0 | 711 | | const Qt::Edges edges = edges_for_direction(direction); |
| | 0 | 712 | | if (edges == Qt::Edges()) { |
| | 0 | 713 | | return false; // unknown direction: nothing to drag from |
| | 0 | 714 | | } |
| | 0 | 715 | | return handle->startSystemResize(edges); |
| | 0 | 716 | | } |