Skip to content

Commit

Permalink
Use bitmap icons in place of Unicode icons where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
davidfstr committed Jan 12, 2024
1 parent e0986cc commit 945c2b1
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 11 deletions.
14 changes: 6 additions & 8 deletions src/crystal/browser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from crystal.browser.addgroup import AddGroupDialog
from crystal.browser.addrooturl import AddRootUrlDialog
from crystal.browser.entitytree import EntityTree, ResourceGroupNode, RootResourceNode
from crystal.browser.icons import TREE_NODE_ICONS
from crystal.browser.preferences import PreferencesDialog
from crystal.browser.tasktree import TaskTree
from crystal.model import (
Expand All @@ -19,6 +20,7 @@
from crystal.ui.actions import Action
from crystal.ui.BetterMessageDialog import BetterMessageDialog
from crystal.ui.log_drawer import LogDrawer
from crystal.ui.tree import DEFAULT_FOLDER_ICON_SET
from crystal.util.finderinfo import get_hide_file_extension
from crystal.util.unicode_labels import decorate_label
from crystal.util.wx_bind import bind
Expand Down Expand Up @@ -195,19 +197,15 @@ def _create_actions(self) -> None:
wx.AcceleratorEntry(wx.ACCEL_CTRL, ord('R')),
self._on_add_url,
enabled=(not self._readonly),
button_label=decorate_label(
RootResourceNode.ICON,
'New &Root URL...',
RootResourceNode.ICON_TRUNCATION_FIX))
button_bitmap=TREE_NODE_ICONS()['entitytree_root_resource'],
button_label='New &Root URL...')
self._new_group_action = Action(wx.ID_ANY,
'New &Group...',
wx.AcceleratorEntry(wx.ACCEL_CTRL, ord('G')),
self._on_add_group,
enabled=(not self._readonly),
button_label=decorate_label(
ResourceGroupNode.ICON,
'New &Group...',
ResourceGroupNode.ICON_TRUNCATION_FIX))
button_bitmap=dict(DEFAULT_FOLDER_ICON_SET())[wx.TreeItemIcon_Normal],
button_label='New &Group...')
self._forget_action = Action(wx.ID_ANY,
'&Forget',
wx.AcceleratorEntry(wx.ACCEL_CTRL, wx.WXK_BACK),
Expand Down
24 changes: 24 additions & 0 deletions src/crystal/browser/icons.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,28 @@ def _add_badge_to_background(background: wx.Bitmap, badge: wx.Bitmap) -> wx.Bitm
return background_plus_badge


def add_transparent_left_border(original: wx.Bitmap, thickness: int) -> wx.Bitmap:
if thickness == 0:
return original

bordered = wx.Bitmap()
success = bordered.Create(
original.Width + thickness,
original.Height,
original.Depth)
if not success:
raise Exception('Failed to create a wx.Bitmap')

dc = wx.MemoryDC(bordered)
dc.Clear() # fill bitmap with white
dc.DrawBitmap(
original,
x=thickness,
y=0,
useMask=True)
dc.SelectObject(wx.NullBitmap) # commit changes to bordered
bordered.SetMaskColour(wx.Colour(255, 255, 255)) # replace white with transparent
return bordered


# ------------------------------------------------------------------------------
15 changes: 14 additions & 1 deletion src/crystal/ui/actions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from crystal.browser.icons import add_transparent_left_border
from crystal.util.wx_bind import bind
from crystal.util.xos import is_mac_os
from crystal.util.xos import is_mac_os, is_windows
from typing import Callable, List, Optional
import wx

Expand All @@ -21,12 +22,14 @@ def __init__(self,
accel: Optional[wx.AcceleratorEntry]=None,
action_func: Optional[Callable[[wx.CommandEvent], None]]=None,
enabled: bool=True,
button_bitmap: Optional[wx.Bitmap]=None,
button_label: str=''):
self._menuitem_id = menuitem_id
self._label = label
self._accel = accel
self._action_func = action_func
self._enabled = enabled
self._button_bitmap = button_bitmap
self._button_label = button_label

self._menuitems = [] # type: List[wx.MenuItem]
Expand Down Expand Up @@ -78,6 +81,16 @@ def create_button(self, *args, **kwargs) -> wx.Button:
button_label = wx.Control.RemoveMnemonics(button_label) # reinterpret

button = wx.Button(*args, label=button_label, **kwargs)
if self._button_bitmap is not None:
button.SetBitmap(add_transparent_left_border(
self._button_bitmap,
# Alter left margin for bitmap to be reasonable on Windows.
# Initially it is 0.
8+6 if is_windows() else 0
))
if is_windows():
# Decrease bitmap-to-text spacing on Windows to be reasonable
button.SetBitmapMargins((-6, 0))
# (NOTE: Do NOT set accelerator for the button because it does not
# work on consistently on macOS. Any action added as a button
# should also be added as a menuitem too, and we CAN set an
Expand Down
4 changes: 2 additions & 2 deletions src/crystal/ui/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
_DEFAULT_TREE_ICON_SIZE = (16,16)

_DEFAULT_FOLDER_ICON_SET_CACHED = None
def _DEFAULT_FOLDER_ICON_SET() -> IconSet:
def DEFAULT_FOLDER_ICON_SET() -> IconSet:
global _DEFAULT_FOLDER_ICON_SET_CACHED # necessary to write to a module global
if not _DEFAULT_FOLDER_ICON_SET_CACHED:
_DEFAULT_FOLDER_ICON_SET_CACHED = (
Expand Down Expand Up @@ -258,7 +258,7 @@ def _set_icon_set(self,
effective_value = (
value
if value is not None else (
_DEFAULT_FOLDER_ICON_SET()
DEFAULT_FOLDER_ICON_SET()
if self.expandable
else _DEFAULT_FILE_ICON_SET()
)
Expand Down

0 comments on commit 945c2b1

Please sign in to comment.