J.S. Cruz

Modifying Kate sessions Plasma widget

Something that annoys me in Plasma’s Kate sessions widget is that, if you want to start a new session, you have to click on the button to start it, instead of being able to just hit Return after you type the new session’s name.

This seems like it should be pretty easy to fix, so let’s do it!

(N.B.: plasmoid, widget, addon or applet all mean the same thing.)

Setup

First, since I don’t want to pollute/mess with my Plasma installation, I’ll download KDE Neon’s developer edition, which should include all development libraries we need out of the gate.

Next, we need to get the source code. All KDE software lives in Invent. Since we’re dealing with a Plasma widget, the code we’re looking for is probably under the Plasma group. Of course, there are a lot of different projects here, but Plasma Add-ons seems generic enough that it could contain what we’re looking for.

There’s a lot of stuff here, but poking around we see a “Kate sessions” inside “applets”, which is exactly what we want.

False start

After cloning the repository, and without having read the documentation :) my first thought was, since there’s a CMakeList.txt, to compile the project using cmake: mkdir build && cmake -B build

In hindsight, I don’t really know what I was expecting with this, since there’s really nothing to compile, given that it’s a QML project. Still, I wouldn’t expect to get this error:

Kate sessions applet compilation error: ECM version mismatch
Shouldn't Neon's dev edition have all the latest packages?

Since I didn’t know any better at this point, I compiled and installed the latest ECM package. Running cmake again, I got a different error:

Kate sessions applet compilation error: Qt version mismatch
Caught in the middle of the Qt6 transition

At this point, I gave up. Since I figured Neon’s developer edition would have the latest development packages, getting error after error got me thinking that I was doing something wrong.

Back on track

QML is not compiled software, or at the very least the applet we’re trying to change isn’t, so trying to compile it is useless. I wanted to see what “the final package” looked like, so I downloaded a random plasmoid from KDE’s store. Once I saw that a packaged applet is just a tar.gz file with the applet’s source code, I tried to zip the katesessions/ folder and load it locally.

Loading local widget
Loading local widget

Before doing this, make sure to change the applet’s ID in metadata.json, so there are no conflicts between it and the installed version. Also, make a small, user-facing change (I changed the name in metadata.json) to distinguish the applet from the system’s when loading.

When you go to load the applet from a local folder, the selection dialog filters by “plasmoid”. Remove the filter to reveal the tar.gz file and select it. For some reason, loading this way can sometimes fail, so you might have to repeat this a few times before the changed applet appears in Plasma’s widget window.

Change

Now, I don’t really know QML all that well, but I watched a small, excellent tutorial by KDAB, so I have some idea of what’s going on. Namely, in this file, we see a row with a text field and two buttons, which looks like the widget’s interface:

Kate sessions widget window
Kate sessions widget window

Implementing our change is as easy as catching Return and just executing the code already defined for onClicked:

 1diff --git a/applets/katesessions/contents/ui/KateSessionsItemDelegate.qml b/applets/katesessions/contents/ui/KateSessionsItemDelegate.qml
 2index fdaabf8b8..4e174686a 100644
 3--- a/applets/katesessions/contents/ui/KateSessionsItemDelegate.qml
 4+++ b/applets/katesessions/contents/ui/KateSessionsItemDelegate.qml
 5@@ -91,6 +91,7 @@ PlasmaComponents.ListItem {
 6                     placeholderText: i18n("Session name")
 7                     clearButtonShown: true
 8                     Layout.fillWidth: true
 9+                    Keys.onReturnPressed: {menuItem.itemSelected(sessionname.text.replace(/^\s+|\s+$/g, '')); showInput=false;}
10                 }
11
12                 PlasmaComponents3.ToolButton {

It’s not ideal to repeat code that’s doing the same thing, but I’m not sure how to factor this into a function. I tried following Qt’s documentation about this, but I always ended up getting “reference not defined” errors, which I couldn’t manage to fix.

While I was at it, I also changed the text field to automatically be in focus when the user clicks on “New session”:

 1diff --git a/applets/katesessions/contents/ui/KateSessionsItemDelegate.qml b/applets/katesessions/contents/ui/KateSessionsItemDelegate.qml
 2index 4e174686a..a12f9ed1d 100644
 3--- a/applets/katesessions/contents/ui/KateSessionsItemDelegate.qml
 4+++ b/applets/katesessions/contents/ui/KateSessionsItemDelegate.qml
 5@@ -34,7 +34,8 @@ PlasmaComponents.ListItem {
 6             if (profileIdentifier !== "")
 7                 menuItem.itemSelected(profileIdentifier);
 8             else {
 9-                showInput=true;
10+                showInput=true;
11+                sessionname.forceActiveFocus();
12             }
13         }
14         onEntered: menuListView.currentIndex = index

and, if the user inputs a session name and then clicks on cancel, the input field should be cleared:

 1diff --git a/applets/katesessions/contents/ui/KateSessionsItemDelegate.qml b/applets/katesessions/contents/ui/KateSessionsItemDelegate.qml
 2index a12f9ed1d..be8793e97 100644
 3--- a/applets/katesessions/contents/ui/KateSessionsItemDelegate.qml
 4+++ b/applets/katesessions/contents/ui/KateSessionsItemDelegate.qml
 5@@ -107,7 +107,10 @@ PlasmaComponents.ListItem {
 6
 7                 PlasmaComponents3.ToolButton {
 8                     icon.name: "dialog-cancel"
 9-                    onClicked: showInput=false
10+                    onClicked: {
11+                        showInput=false;
12+                        sessionname.text='';
13+                    }
14
15                     PlasmaComponents3.ToolTip {
16                         text: i18n("Cancel session creation")

Conclusion

As per the documentation, to test the changes, run plasmoidviewer -a APPLET_ID.

You can also install the applet as described above. This will store it in ~/.local/share/plasma/plasmoids/APPLET_ID. You can also just copy the source code folder here, renaming it to APPLET_ID. Removing this folder also removes the widget.

As a shortcut, you can also directly change files in this directory to change the widget without having repeat the loading process. Make sure to reload Plasma (e.g., $ plasmashell --replace) after every change.

I submitted this change to KDE as a patch. Here’s hoping they’ll accept it.

Tags: #kde #kate #plasma