Przeglądaj źródła

LibGUI+WindowServer: Allow applications to set custom cursor bitmaps

This will allow e.g PaintBrush to use custom cursors for each tool.
Shannon Booth 5 lat temu
rodzic
commit
df43e09433

+ 12 - 1
Libraries/LibGUI/Window.cpp

@@ -209,10 +209,21 @@ void Window::set_override_cursor(StandardCursor cursor)
 {
     if (!is_visible())
         return;
-    if (m_override_cursor == cursor)
+    if (!m_custom_cursor && m_override_cursor == cursor)
         return;
     WindowServerConnection::the().send_sync<Messages::WindowServer::SetWindowOverrideCursor>(m_window_id, (u32)cursor);
     m_override_cursor = cursor;
+    m_custom_cursor = nullptr;
+}
+
+void Window::set_override_cursor(const Gfx::Bitmap& cursor)
+{
+    if (!is_visible())
+        return;
+    if (&cursor == m_custom_cursor.ptr())
+        return;
+    m_custom_cursor = &cursor;
+    WindowServerConnection::the().send_sync<Messages::WindowServer::SetWindowCustomOverrideCursor>(m_window_id, m_custom_cursor->to_shareable_bitmap(WindowServerConnection::the().server_pid()));
 }
 
 void Window::event(Core::Event& event)

+ 2 - 0
Libraries/LibGUI/Window.h

@@ -163,6 +163,7 @@ public:
     void set_base_size(const Gfx::Size&);
 
     void set_override_cursor(StandardCursor);
+    void set_override_cursor(const Gfx::Bitmap&);
 
     void set_icon(const Gfx::Bitmap*);
     void apply_icon();
@@ -204,6 +205,7 @@ private:
     RefPtr<Gfx::Bitmap> m_front_bitmap;
     RefPtr<Gfx::Bitmap> m_back_bitmap;
     RefPtr<Gfx::Bitmap> m_icon;
+    RefPtr<Gfx::Bitmap> m_custom_cursor;
     int m_window_id { 0 };
     float m_opacity_when_windowless { 1.0f };
     RefPtr<Widget> m_main_widget;

+ 19 - 0
Services/WindowServer/ClientConnection.cpp

@@ -578,6 +578,25 @@ OwnPtr<Messages::WindowServer::SetWindowOverrideCursorResponse> ClientConnection
     return make<Messages::WindowServer::SetWindowOverrideCursorResponse>();
 }
 
+OwnPtr<Messages::WindowServer::SetWindowCustomOverrideCursorResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowCustomOverrideCursor& message)
+{
+    auto it = m_windows.find(message.window_id());
+    if (it == m_windows.end()) {
+        did_misbehave("SetWindowCustomOverrideCursor: Bad window ID");
+        return nullptr;
+    }
+
+    auto& window = *(*it).value;
+    if (!message.cursor().is_valid()) {
+        did_misbehave("SetWindowCustomOverrideCursor: Bad cursor");
+        return nullptr;
+    }
+
+    window.set_override_cursor(Cursor::create(*message.cursor().bitmap()));
+    Compositor::the().invalidate_cursor();
+    return make<Messages::WindowServer::SetWindowCustomOverrideCursorResponse>();
+}
+
 OwnPtr<Messages::WindowServer::SetWindowHasAlphaChannelResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowHasAlphaChannel& message)
 {
     auto it = m_windows.find(message.window_id());

+ 1 - 0
Services/WindowServer/ClientConnection.h

@@ -114,6 +114,7 @@ private:
     virtual OwnPtr<Messages::WindowServer::GetWallpaperResponse> handle(const Messages::WindowServer::GetWallpaper&) override;
     virtual OwnPtr<Messages::WindowServer::SetResolutionResponse> handle(const Messages::WindowServer::SetResolution&) override;
     virtual OwnPtr<Messages::WindowServer::SetWindowOverrideCursorResponse> handle(const Messages::WindowServer::SetWindowOverrideCursor&) override;
+    virtual OwnPtr<Messages::WindowServer::SetWindowCustomOverrideCursorResponse> handle(const Messages::WindowServer::SetWindowCustomOverrideCursor&) override;
     virtual OwnPtr<Messages::WindowServer::PopupMenuResponse> handle(const Messages::WindowServer::PopupMenu&) override;
     virtual OwnPtr<Messages::WindowServer::DismissMenuResponse> handle(const Messages::WindowServer::DismissMenu&) override;
     virtual OwnPtr<Messages::WindowServer::SetWindowIconBitmapResponse> handle(const Messages::WindowServer::SetWindowIconBitmap&) override;

+ 2 - 1
Services/WindowServer/WindowServer.ipc

@@ -73,7 +73,7 @@ endpoint WindowServer = 2
     DismissMenu(i32 menu_id) => ()
 
     AsyncSetWallpaper(String path) =|
-    
+
     SetBackgroundColor(String background_color)  => ()
     SetWallpaperMode(String mode)  => ()
 
@@ -82,6 +82,7 @@ endpoint WindowServer = 2
 
     GetWallpaper() => (String path)
     SetWindowOverrideCursor(i32 window_id, i32 cursor_type) => ()
+    SetWindowCustomOverrideCursor(i32 window_id, Gfx::ShareableBitmap cursor) => ()
 
     StartDrag(String text, String data_type, String data, i32 bitmap_id, Gfx::Size bitmap_size) => (bool started)