Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
[Share Code] theme_utils.py
-
theme-utils
This is a module called
theme_utils
that makes it easy to work with themes, as well as making it easy to style UI components with these themes.The module
The module consists of two main parts, but they're in the same namespace.
- Utilities for getting attributes of the current user theme. These include ready-made functions for accessing the most useful and common attribute of the theme, as well as lower-level functions like
load_theme
that can help with more specific use cases (like getting the colors of the code syntax). These include:load_theme
- return a Pythondict
representing the current themeget_theme_name
- return the name of the current themeget_tint
- return the tint color of the current themeget_color_scheme
- return a color scheme to go with the current theme. This consists of four colors, the editor background color, the library background color, the top bar's color, and the color of the backgrounder tabs. These are sorted by "intensity," which is dark to light for "dark themes" and light to dark for "light themes"theme_is_light
andtheme_is_dark
- is the theme a light theme, or a dark theme. Judged by averaging intensities of the 4 main colors, fromget_color_schemes
- Utilities for styling
ui
s to match the current theme.style_ui(view)
, andview
(and its children) will be styled to match the current theme. A few special things are happening in the background here.- Certain elements, like
ui.View
,ui.Label
,ui.Slider
, andui.NavigationView
have their backgrounds set to the "dominant color" (the first in theget_color_scheme
). These are elements that either are "background elements," elements which serve primarily as a background for other elements, or are elements whose background isn't a part of the element. The background of aui.Slider
should appear opaque, but the background of aui.Button
or aui.TextField
should appear as part of the element, and therefore in a color that stands out. Because these colors are pulled directly from the themes, in high-contrast themes, this contrast will be more, but in a theme with very little contrast, this contrast will be less. - You can choose to have elements which have already been styled not have their styles overridden by setting
respect_changes
toTrue
- Certain elements, like
Examples
Solarized Light:
Tomorrow Night:
Running the script as-is will output some useful information about the current theme:
Custom themes
All of these functions work flawlessly with custom themes, built with @omz's Pythonista Theme Editor.
Pacific Dark:
The code
The code is here. I haven't include the
pyui
file I used for testing, but that's easy enough to make yourself.This will likely be merged into
Pythonista-Tweaks
as part of the theme functionality, but that will be after there is much more control over these things. - Utilities for getting attributes of the current user theme. These include ready-made functions for accessing the most useful and common attribute of the theme, as well as lower-level functions like
-
This is looking really nice!
The
_clean_json
function shouldn't be necessary anymore with the next build btw. ;)One thing that seems to be missing is that labels should probably have light text when using a dark theme.
-
@omz thanks, I'll add that. Also thanks for fixing the trailing commas ;). My custom theme will likely still have them because it was modified from Oceanic, but that's on me to fix :)
-
@omz updated, but can't seem to find a way to set the color for a DatePicker.
-
@omz found it :)
>>> uidp=ui.DatePicker() >>> ocdp=ObjCInstance(uidp) >>> color=ObjCClass('UIColor').whiteColor() >>> ocdp.setValue_forKey_(color,'textColor') >>> uidp.present('sheet')