Skip to content

gtk4: Implement auto-colouring of toolbar icons #27989

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions lib/matplotlib/backends/_backend_gtk.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def _shutdown_application(app):
def _create_application():
global _application

created = False
if _application is None:
app = Gio.Application.get_default()
if app is None or getattr(app, '_created_by_matplotlib', False):
Expand All @@ -60,10 +61,11 @@ def _create_application():
_application.connect('shutdown', _shutdown_application)
_application.register()
cbook._setup_new_guiapp()
created = True
else:
_application = app

return _application
return _application, created


def mpl_to_gtk_cursor_name(mpl_cursor):
Expand Down Expand Up @@ -137,7 +139,16 @@ class _FigureManagerGTK(FigureManagerBase):
def __init__(self, canvas, num):
self._gtk_ver = gtk_ver = Gtk.get_major_version()

app = _create_application()
app, created = _create_application()
if created:
if gtk_ver == 3:
theme = Gtk.IconTheme.get_default()
_add_icon_theme = theme.append_search_path
else:
theme = Gtk.IconTheme.get_for_display(Gdk.Display.get_default())
_add_icon_theme = theme.add_search_path
_add_icon_theme(str(cbook._get_data_path('images/icons')))

self.window = Gtk.Window()
app.add_window(self.window)
super().__init__(canvas, num)
Expand Down
23 changes: 16 additions & 7 deletions lib/matplotlib/backends/backend_gtk3.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,8 @@ def __init__(self, canvas):
if text is None:
self.insert(Gtk.SeparatorToolItem(), -1)
continue
image = Gtk.Image.new_from_gicon(
Gio.Icon.new_for_string(
str(cbook._get_data_path('images',
f'{image_file}-symbolic.svg'))),
image = Gtk.Image.new_from_icon_name(
f'org.matplotlib.Matplotlib.{image_file}',
Gtk.IconSize.LARGE_TOOLBAR)
self._gtk_ids[text] = button = (
Gtk.ToggleToolButton() if callback in ['zoom', 'pan'] else
Expand Down Expand Up @@ -399,6 +397,12 @@ def __init__(self, toolmanager):
self._groups = {}
self._toolitems = {}

def _get_image_filename(self, tool):
if (fname := super()._get_image_filename(tool)) is not None:
return fname
if tool.image.startswith('mpl-data/'):
return os.path.basename(tool.image)

def add_toolitem(self, name, group, position, image_file, description,
toggle):
if toggle:
Expand All @@ -408,9 +412,14 @@ def add_toolitem(self, name, group, position, image_file, description,
button.set_label(name)

if image_file is not None:
image = Gtk.Image.new_from_gicon(
Gio.Icon.new_for_string(image_file),
Gtk.IconSize.LARGE_TOOLBAR)
if os.path.isabs(image_file):
image = Gtk.Image.new_from_gicon(
Gio.Icon.new_for_string(image_file),
Gtk.IconSize.LARGE_TOOLBAR)
else:
image = Gtk.Image.new_from_icon_name(
f'org.matplotlib.Matplotlib.{image_file}',
Gtk.IconSize.LARGE_TOOLBAR)
button.set_icon_widget(image)

if position is None:
Expand Down
7 changes: 6 additions & 1 deletion lib/matplotlib/backends/backend_gtk3agg.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from .. import cbook, transforms
from . import backend_agg, backend_gtk3
from .backend_gtk3 import Gtk, _BackendGTK3
from .backend_gtk3 import GLib, Gtk, _BackendGTK3

import cairo # Presence of cairo is already checked by _backend_gtk.

Expand All @@ -14,6 +14,11 @@ def __init__(self, figure):
self._bbox_queue = []

def on_draw_event(self, widget, ctx):
if self._idle_draw_id:
GLib.source_remove(self._idle_draw_id)
self._idle_draw_id = 0
self.draw()

scale = self.device_pixel_ratio
allocation = self.get_allocation()
w = allocation.width * scale
Expand Down
7 changes: 6 additions & 1 deletion lib/matplotlib/backends/backend_gtk3cairo.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
from contextlib import nullcontext

from .backend_cairo import FigureCanvasCairo
from .backend_gtk3 import Gtk, FigureCanvasGTK3, _BackendGTK3
from .backend_gtk3 import GLib, Gtk, FigureCanvasGTK3, _BackendGTK3


class FigureCanvasGTK3Cairo(FigureCanvasCairo, FigureCanvasGTK3):
def on_draw_event(self, widget, ctx):
if self._idle_draw_id:
GLib.source_remove(self._idle_draw_id)
self._idle_draw_id = 0
self.draw()

with (self.toolbar._wait_cursor_for_draw_cm() if self.toolbar
else nullcontext()):
self._renderer.set_context(ctx)
Expand Down
22 changes: 15 additions & 7 deletions lib/matplotlib/backends/backend_gtk4.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,8 @@ def __init__(self, canvas):
if text is None:
self.append(Gtk.Separator())
continue
image = Gtk.Image.new_from_gicon(
Gio.Icon.new_for_string(
str(cbook._get_data_path('images',
f'{image_file}-symbolic.svg'))))
image = Gtk.Image.new_from_icon_name(
f'org.matplotlib.Matplotlib.{image_file}-symbolic')
self._gtk_ids[text] = button = (
Gtk.ToggleButton() if callback in ['zoom', 'pan'] else
Gtk.Button())
Expand Down Expand Up @@ -400,6 +398,7 @@ def __init__(self, toolmanager):
ToolContainerBase.__init__(self, toolmanager)
Gtk.Box.__init__(self)
self.set_property('orientation', Gtk.Orientation.HORIZONTAL)
self.add_css_class('toolbar')

# Tool items are created later, but must appear before the message.
self._tool_box = Gtk.Box()
Expand All @@ -421,18 +420,27 @@ def __init__(self, toolmanager):
self._message.set_justify(Gtk.Justification.RIGHT)
self.append(self._message)

def _get_image_filename(self, tool):
if (fname := super()._get_image_filename(tool)) is not None:
return fname
if tool.image.startswith('mpl-data/'):
return os.path.basename(tool.image)

def add_toolitem(self, name, group, position, image_file, description,
toggle):
if toggle:
button = Gtk.ToggleButton()
else:
button = Gtk.Button()
button.set_label(name)
button.add_css_class('flat')

if image_file is not None:
image = Gtk.Image.new_from_gicon(
Gio.Icon.new_for_string(image_file))
if os.path.isabs(image_file):
image = Gtk.Image.new_from_gicon(
Gio.Icon.new_for_string(image_file))
else:
image = Gtk.Image.new_from_icon_name(
f'org.matplotlib.Matplotlib.{image_file}-symbolic')
button.set_child(image)
button.add_css_class('image-button')

Expand Down
7 changes: 6 additions & 1 deletion lib/matplotlib/backends/backend_gtk4agg.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from .. import cbook
from . import backend_agg, backend_gtk4
from .backend_gtk4 import Gtk, _BackendGTK4
from .backend_gtk4 import GLib, Gtk, _BackendGTK4

import cairo # Presence of cairo is already checked by _backend_gtk.

Expand All @@ -11,6 +11,11 @@ class FigureCanvasGTK4Agg(backend_agg.FigureCanvasAgg,
backend_gtk4.FigureCanvasGTK4):

def on_draw_event(self, widget, ctx):
if self._idle_draw_id:
GLib.source_remove(self._idle_draw_id)
self._idle_draw_id = 0
self.draw()

scale = self.device_pixel_ratio
allocation = self.get_allocation()

Expand Down
7 changes: 6 additions & 1 deletion lib/matplotlib/backends/backend_gtk4cairo.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
from contextlib import nullcontext

from .backend_cairo import FigureCanvasCairo
from .backend_gtk4 import Gtk, FigureCanvasGTK4, _BackendGTK4
from .backend_gtk4 import GLib, Gtk, FigureCanvasGTK4, _BackendGTK4


class FigureCanvasGTK4Cairo(FigureCanvasCairo, FigureCanvasGTK4):
_context_is_scaled = True

def on_draw_event(self, widget, ctx):
if self._idle_draw_id:
GLib.source_remove(self._idle_draw_id)
self._idle_draw_id = 0
self.draw()

with (self.toolbar._wait_cursor_for_draw_cm() if self.toolbar
else nullcontext()):
self._renderer.set_context(ctx)
Expand Down
1 change: 0 additions & 1 deletion lib/matplotlib/mpl-data/images/back-symbolic.svg

This file was deleted.

1 change: 0 additions & 1 deletion lib/matplotlib/mpl-data/images/filesave-symbolic.svg

This file was deleted.

1 change: 0 additions & 1 deletion lib/matplotlib/mpl-data/images/forward-symbolic.svg

This file was deleted.

1 change: 0 additions & 1 deletion lib/matplotlib/mpl-data/images/help-symbolic.svg

This file was deleted.

1 change: 0 additions & 1 deletion lib/matplotlib/mpl-data/images/home-symbolic.svg

This file was deleted.

1 change: 0 additions & 1 deletion lib/matplotlib/mpl-data/images/move-symbolic.svg

This file was deleted.

1 change: 0 additions & 1 deletion lib/matplotlib/mpl-data/images/subplots-symbolic.svg

This file was deleted.

1 change: 0 additions & 1 deletion lib/matplotlib/mpl-data/images/zoom_to_rect-symbolic.svg

This file was deleted.