Skip to content
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

Toolkit docs #408

Merged
merged 2 commits into from
Jan 1, 2025
Merged
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
Binary file added docs/_static/images/text_power_menu.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
75 changes: 70 additions & 5 deletions docs/manual/how_to/popup.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Currently, the following controls are provided:
- ``PopupText``: a simple text display object
- ``PopupImage``: a control to display an image
- ``PopupSlider``: a control to draw a line which marks a particular value (e.g. volume level)
- ``PopupCircularProgress``: a control to draw a curved line showing progress etc.
- ``PopupWidget``: a control to display a Qtile widget in the popup

Configuration options for these controls can be found on
Expand All @@ -55,7 +56,7 @@ Below is an example of creating a power menu in your ``config.py``.

.. code:: python

from qtile_extras.popup.toolkit import (
from qtile_extras.popup import (
PopupRelativeLayout,
PopupImage,
PopupText
Expand Down Expand Up @@ -154,7 +155,7 @@ Below is a quick example for displaying a number of graph widgets in a popup:
.. code:: python

from libqtile import widget
from qtile_extras.popup.toolkit import (
from qtile_extras.popup import (
PopupRelativeLayout,
PopupWidget
)
Expand Down Expand Up @@ -216,7 +217,7 @@ be updated in the same call.

.. code:: python

from qtile_extras.popup.toolkit import (
from qtile_extras.popup import (
PopupRelativeLayout,
PopupText
)
Expand Down Expand Up @@ -267,7 +268,7 @@ For example, to make the ``Clock`` widget show the long date when clicked:
from libqtile import widget

from qtile_extras import widget as extrawidgets
from qtile_extras.popup.toolkit import PopupRelativeLayout, PopupText, PopupWidget
from qtile_extras.popup import PopupRelativeLayout, PopupText, PopupWidget
from qtile_extras.widget.mixins import ExtendedPopupMixin


Expand Down Expand Up @@ -321,4 +322,68 @@ For example, to make the ``Clock`` widget show the long date when clicked:

Putting ``extended_clock`` in your bar and clicking on the clock will give you this:

.. image:: /_static/images/extended_popup_clock.png
.. image:: /_static/images/extended_popup_clock.png


Building simple text menus
==========================

The toolkit also contains some basic classes to simplify the creation of text based menus.
The primary use of these classes is to provide context menus for widgets (e.g. ``StatusNotifier``)
but they can also be easily incorporated directly in config files.

For example, to recreate the power menu above using text, you could define the following:

.. code:: python

from libqtile.lazy import lazy

from qtile_extras.popup import PopupMenu, PopupMenuItem, PopupMenuSeparator


@lazy.function
def show_text_power_menu(qtile):
items = [
PopupMenuItem(text="Power Menu", enabled=False),
PopupMenuSeparator(),
PopupMenuItem(
text="Lock",
mouse_callbacks={
"Button1": lazy.spawn("/path/to/lock_cmd")
}
),
PopupMenuItem(
text="Sleep",
mouse_callbacks={
"Button1": lazy.spawn("/path/to/lock_cmd")
}
),
PopupMenuItem(
text="Shutdown",
highlight="900",
mouse_callbacks={
"Button1": lazy.shutdown()
}
),
]
menu = PopupMenu.generate(qtile, menuitems=items, border_width=2)
menu.show(centered=True)

keys = [
...
Key([mod, "shift"], "q", show_text_power_menu)
...
]

Pressing ``Mod + shift + B`` will display the following:

.. image:: /_static/images/text_power_menu.png

Menu items have a default blue/green highlight but this was overriden to show red for
the shutdown command.

Note that the menu items can be configured individually. Configuration options for the layout
(border etc.) are passed to the ``generate`` method.

Configuration options for the menu objects can be found on
:ref:`the reference page <ref-popup-menus>`.
19 changes: 18 additions & 1 deletion docs/manual/ref/popup.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,21 @@ and have each of these react to input events or display information dynamically.
:baseclass: qtile_extras.popup.toolkit._PopupWidget
:exclude-base:
:no-commands:
:show-config:
:show-config:


.. _ref-popup-menus:

Popup Toolkit Menus
===================

These are basic text menus. The layout is generated automatically and users are only
required to provide the menu items.

.. qtile_class:: qtile_extras.popup.menu.PopupMenu
:methods: generate,from_dbus_menu

.. qtile_class:: qtile_extras.popup.menu.PopupMenuItem

.. qtile_class:: qtile_extras.popup.menu.PopupMenuSeparator

47 changes: 47 additions & 0 deletions qtile_extras/popup/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Copyright (c) 2024 elParaguayo
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from qtile_extras.popup.menu import PopupMenu, PopupMenuItem, PopupMenuSeparator
from qtile_extras.popup.toolkit import (
PopupAbsoluteLayout,
PopupCircularProgress,
PopupGridLayout,
PopupImage,
PopupRelativeLayout,
PopupSlider,
PopupText,
PopupWidget,
)

__all__ = [
# Basic toolkit layouts
"PopupAbsoluteLayout",
"PopupGridLayout",
"PopupRelativeLayout",
# Basic toolkit widgets
"PopupCircularProgress",
"PopupImage",
"PopupSlider",
"PopupText",
"PopupWidget",
# Menu toolkit
"PopupMenu",
"PopupMenuItem",
"PopupMenuSeparator",
]
24 changes: 19 additions & 5 deletions qtile_extras/popup/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,13 @@ class PopupMenu(PopupGridLayout):
A class for creating menus.

The menu should be created via one of two classmethods:
`from_dbus_menu` or `generate`. The former accepts a list of
`DBusMenuItem` objects and the second accepts a list of
`PopupMenuItem` and `PopupMenuSeparator` objects.
``from_dbus_menu`` or ``generate``. The former accepts a list of
``DBusMenuItem`` objects and the second accepts a list of
``PopupMenuItem`` and ``PopupMenuSeparator`` objects.

The menu is created as a PopupGridLayout object. Therefore, if
using the `generate` method, object sizes can be changed using
the `row_span` attribute. By default, a text item will be twice
using the ``generate`` method, object sizes can be changed using
the ``row_span`` attribute. By default, a text item will be twice
the height of a separator.
"""

Expand All @@ -182,6 +182,12 @@ def __init__(self, qtile, controls, **config):

@classmethod
def from_dbus_menu(cls, qtile, dbusmenuitems, **config):
"""
Create a ``PopupMenu`` instance a DBus menu.

The ``dbusmenuitems`` need to be created from ``DBusMenu.parse_menu`` as this
will generate the ``DBusMenuItem`` objects needed to create a menu.
"""
menuitems = []
prev_sep = False

Expand Down Expand Up @@ -227,6 +233,14 @@ def from_dbus_menu(cls, qtile, dbusmenuitems, **config):

@classmethod
def generate(cls, qtile, menuitems, **config):
"""
Create a ``PopupMenu`` instance from the provided ``menuitems``.

Users must pass the ``qtile`` instance as well as the list of items.

``menuitems`` should be a list of ``PopupMenuItem`` and ``PopupMenuSeparator``
instances.
"""
row_count = 0
for item in menuitems:
item.row = row_count
Expand Down
Loading