beetlebug

Linux features

This page shows the parts of a Linux desktop that a ztk script reaches: the two display protocols, the button in the dock, the tray, the file dialogs of the desktop, drags with other apps, the icon of a window, and the display of a device with no desktop. Each part has a script that you can run with ztk.

Some commands exist on each platform but work only on some. Test the platform first with ztk::has:

Test 1 on Linux when
ztk::has tray ztk::tray can show an icon: a Wayland session with a session bus.
ztk::has taskbar ztk::taskbar sends its state to the dock: a Wayland session.
ztk::has native-menus Never: ztk draws the menus.

Wayland and X11

ztk has its own client of each protocol, with no C library. Wayland is the full backend. The X11 client is for a desktop with no Wayland compositor.

Wayland X11
Windows One window for each toplevel One window for each toplevel
Renderer Vulkan, or the CPU renderer The CPU renderer
Scale The scale of the output, also fractional Xft.dpi of the X resources divided by 96, also fractional
Menus Popup surfaces that can go past the window Inside the window
Clipboard Yes Text (CLIPBOARD)
Input methods Yes (text-input-v3) No
Drags with other apps Yes No
Tray, portal dialogs Yes, with a session bus No
Dark mode and accent color of the desktop Yes, with the Settings portal No
wm iconphoto Yes (xdg-toplevel-icon-v1) Yes (_NET_WM_ICON)

On X11, set the scale with Xft.dpi in the X resources. For example, put Xft.dpi: 144 in ~/.Xresources for the scale 1.5, and load the file with xrdb -merge ~/.Xresources.

ztk uses Wayland when WAYLAND_DISPLAY is set, and X11 when only DISPLAY is set. To use X11 in a Wayland session, for example through XWayland, unset the variable:

env -u WAYLAND_DISPLAY ztk app.tcl

The button of the app in the dock

ztk::taskbar shows a progress bar, a badge, and a request for attention on the button of the app in the dock or the task manager of the desktop. The task manager of KDE Plasma, Dash to Dock of GNOME, and Plank show them.

ztk::taskbar badge 3
ztk::taskbar progress 0.4
after 3000 {ztk::taskbar progress none; ztk::taskbar attention}

The dock finds the button from the desktop entry of the app, APP_ID.desktop, where APP_ID is the app id of the window: ztk, or the name of a bundled app (Make an app). The bar has no colors for -state paused and -state error. A request for attention marks the button until the window gets the keyboard.

ztk::taskbar has each subcommand.

The title bar on GNOME and weston

KDE Plasma, sway, Hyprland, and most other Wayland compositors draw the frame of each window. GNOME and weston draw no frame for an app. On those desktops ztk draws a title bar at the top of each toplevel, in the look of the theme:

The widget gallery on weston with the title bar of ztk: the title in the middle, and the buttons minimize, maximize, and close on the right
The widget gallery on weston, with the title bar that ztk draws.
  • The bar shows the text of wm title in the middle.
  • Drag the bar to move the window. Double-click the bar to maximize the window, and again to give it its size back.
  • The buttons on the right minimize, maximize, and close the window. When the window is maximized, the middle button gives the window its size back.
  • The close button runs the script of wm protocol . WM_DELETE_WINDOW, as the close of the desktop does. With no script, it closes the app.
  • Click the bar with mouse button 3 to open the window menu of the compositor.
  • Drag an edge or a corner of the window to change its size. Over an edge, the pointer shows the arrows of a resize.

The bar follows the scheme of the app, which ztk::style scheme gives, so ztk --dark gives a dark bar. The size of wm geometry does not include the bar, as it does not include the frame of a compositor. wm resizable . 0 0 removes the maximize button and the resize edges. A full screen window and a window with wm overrideredirect . 1 have no bar.

Set ZTK_WAYLAND_CSD=1 to get the title bar of ztk on a compositor that draws frames.

An icon in the tray

ztk::tray puts an icon in the tray of the panel, as a StatusNotifierItem on the session bus. A panel that hosts these items, such as the panel of KDE Plasma, shows it.

image create photo dot -width 16 -height 16
dot put #2c7be5 -to 2 2 14 14
menu .traymenu -tearoff 0
.traymenu add command -label "Show" -command {wm deiconify .; raise .}
.traymenu add checkbutton -label "Pause" -variable paused
.traymenu add separator
.traymenu add command -label "Quit" -command exit
if {[ztk::has tray]} {
    ztk::tray create -image dot -tooltip "Backup" -menu .traymenu -command {
        if {"%a" eq "click"} {wm deiconify .; raise .}
    }
}

The panel shows -menu as its own menu, at the right button. The menu goes to the panel as a com.canonical.dbusmenu menu on the session bus, with the labels, the underlined letters, the accelerators, the state of each entry, the indicators of the check and radio entries, the separators, and the cascades. A choice of an entry runs it in the script, as invoke does. The -postcommand of a menu runs each time the panel opens it. A change of the menu shows the next time the panel opens it.

%a is the action: click for the first button and double for the action of the second button of most panels (the middle button). A tray with no -menu gets menu for the right button, and the script can post its own menu then.

The file dialogs of the desktop

tk_getOpenFile, tk_getSaveFile, and tk_chooseDirectory show the file dialog of the desktop through the portal org.freedesktop.portal.FileChooser, when the session bus has the portal org.freedesktop.portal.Desktop. On a desktop with no portal, ztk draws its own file dialog.

ttk::button .open -text "Open..." -command {
    set file [tk_getOpenFile -title "Open a note" \
        -filetypes {{"Text files" {.txt .md}} {"All files" *}}]
    if {$file ne ""} {puts "open $file"}
}
pack .open -padx 20 -pady 20

The filters of -filetypes become the filters of the dialog. tk_messageBox, tk_chooseColor, and tk fontchooser show the dialogs that ztk draws, on each desktop.

Drags with other apps

On Wayland a widget gets files and text from the file manager and other apps with tkdnd::drop_target, and <<Drop>> gets the list of the paths in %D:

ttk::label .drop -text "Drop files here" -padding 40 -relief groove
pack .drop -padx 20 -pady 20 -fill both -expand 1
tkdnd::drop_target register .drop {DND_Files DND_Text}
bind .drop <<Drop>> {.drop configure -text "[llength %D] items"}

ztk::dnd drag drags files or text out of a window. Call it from a binding of <B1-Motion>. It gives copy, move, or none:

set path [file normalize notes.txt]
ttk::label .file -text "notes.txt" -padding 20
pack .file -padx 20 -pady 20
bind .file <B1-Motion> {
    puts [ztk::dnd drag .file -files [list $path]]
}

ztk::dnd has each option and event.

The icon of a window

wm iconphoto sets the icon of the window. Give photos of several sizes, and the desktop picks the best one:

image create photo icon16 -width 16 -height 16
image create photo icon64 -width 64 -height 64
icon16 put #e0803a -to 0 0 16 16
icon64 put #e0803a -to 0 0 64 64
wm iconphoto . -default icon16 icon64

On Wayland the photos go to the compositor with xdg-toplevel-icon-v1. A compositor without that protocol shows the icon of the desktop entry of the app (Make an app). On X11 the photos go in the property _NET_WM_ICON.

Window states

Command The window
wm attributes . -fullscreen 1 Goes to the full screen of the compositor.
wm attributes . -fullscreen 0 Comes back from the full screen.
wm state . zoomed Is maximized. wm state . gives zoomed also after the user maximizes the window in the compositor.
wm state . normal Comes back from the maximized state.
ttk::button .max -text "Maximize" -command {
    if {[wm state .] eq "zoomed"} {wm state . normal} else {wm state . zoomed}
}
pack .max -padx 20 -pady 20

These states are for Wayland. On X11 the window keeps its size.

Wayland has no request to keep a window above the others, so wm attributes . -topmost 1 keeps the value and does not change the window.

Screen readers

Orca and the other assistive tools of Linux read the Tk widgets of a window through AT-SPI, the accessibility interface of the desktop on the session bus:

Widget Orca reads The action of AT-SPI
A label Its text No action
A button Its text, and "push button" Presses the button
A check button, a radio button Its text, and its state Changes the state
An entry, a spinbox, a combobox Its text; the text of an entry with -show stays silent No action
A widget with -state disabled It, and "grayed" No action

The window connects to the accessibility bus when a screen reader runs, or when it starts. Orca follows the keyboard focus of Tk, and reads the widgets in the order of the widget tree. A button with an image and no text has no name, so give it a -text. To show the widgets to a tool that does not ask for accessibility, such as Accerciser, start the program with ZTK_ACCESSIBILITY=1:

ZTK_ACCESSIBILITY=1 ztk app.tcl

Global shortcuts

ztk::hotkey runs a script when a key goes down in any app. On Wayland, ztk asks the desktop for the shortcut through the portal org.freedesktop.portal.GlobalShortcuts. It needs the grant --allow hotkey:

ztk::hotkey add <Control-Alt-k> {wm deiconify .; raise .}
ztk --allow hotkey app.tcl

The portal gives shortcuts only to an app with a desktop file. The name of the file is the app id with .desktop: ztk.desktop for the ztk program, or the name of the program of a bundled app. Put the file in an applications folder, such as ~/.local/share/applications:

[Desktop Entry]
Type=Application
Name=Notes
Exec=/usr/local/bin/notes

The desktop gets each shortcut with the name of add (hotkey1, hotkey2, and so on) as its id, the keys as the proposed trigger, and the app id with the keys as its description. The desktop decides the keys:

  • On a desktop such as KDE Plasma, the portal can show a dialog for the new shortcuts, where the user sets the keys. The portal documentation gives this behavior. It was not checked on KDE Plasma.
  • Hyprland ignores the proposed keys. The user binds the keys to the dispatcher global with the app id and the id, such as ztk:hotkey1, in the config of Hyprland. hyprctl globalshortcuts lists the ids. Hyprland keeps the ids in that list after the app ends.
  • With no portal of global shortcuts, no session bus, or no desktop file, add gives an error, and ztk prints the cause to stderr.

ztk::hotkey gives an error on X11.

A device with no desktop

The direct backend draws on the framebuffer of the kernel, with no Wayland or X server, for a device such as a panel with a touch screen. It reads the keyboard, the mouse, and the touch panel from /dev/input. A touch is button 1.

ztk --direct --direct-unsafe-test-device --fb /dev/fb0 --rotate 90 app.tcl
Flag What it does
--fb DEVICE The framebuffer. The default is /dev/fb0.
--rotate 0|90|180|270 The rotation of the panel.
--touch-debounce MS Joins two touches that come within MS milliseconds.
--tty DEVICE The virtual terminal, such as /dev/tty1.

The direct backend does not take full control of the virtual terminal yet, so it needs the flag --direct-unsafe-test-device. Use it only on a device that can lose its text console.

The next page, Limits, lists what does not work on Linux.