Commit 35f915b9 authored by Lysander Trischler's avatar Lysander Trischler

Prettify buttons and emit click on mouse button release

parent aca5b0f9
......@@ -118,7 +118,8 @@ TODO
* Configuration:
* Everything in twtxt's config file, not in code.
* Define more colors and key bindings.
* Define fancier 88/256 colors.
* Define key bindings.
* Match ``[nick-colors]]`` by feed URLs using the ``[following]`` mapping
internally, but still keep nicks as keys.
......@@ -126,6 +127,8 @@ TODO
* Ability to show the txt's raw text.
* Manage subscriptions.
* Reload feeds.
* Publish own feed.
* Add key bindings to buttons.
WTF!
====
......@@ -329,6 +332,10 @@ Default configuration::
link-focus = light blue,underline; white
error-normal = white,bold; dark red
error-focus = dark red,bold; white
publish-normal = white; black
publish-focus = black; white
cancel-normal = white; black
cancel-focus = black; white
The ``-normal`` suffix indicates a list entry which doesn't have a focus, the
``-focus`` suffix on the other hand will be applied, when the list entry is
......
......@@ -461,8 +461,8 @@ class TwtForm(urwid.ListBox, TwtFormatter):
preview_list_walker = urwid.SimpleFocusListWalker(preview_widgets)
preview_box_adapter = widgets.DelegatingBoxAdapter(widgets.VimListBox(preview_list_walker),
rows_calculating_delegate_widget)
publish = urwid.Button("Publish Twt", on_press=self.publish_twt)
cancel = urwid.Button("Cancel", on_press=lambda *_: frame.body.pop_widget())
publish = widgets.PushButton("Publish Twt", on_press=self.publish_twt)
cancel = widgets.PushButton("Cancel", on_press=lambda *_: frame.body.pop_widget())
super().__init__(rendered_conversation + [
urwid.LineBox(self._text_edit, title="Reply" if original_twt else "New Twt", title_align="left"),
urwid.LineBox(self._created_at_edit, title="Created at", title_align="left"),
......@@ -470,8 +470,8 @@ class TwtForm(urwid.ListBox, TwtFormatter):
title="Reply Preview" if original_twt else "New Twt Preview",
title_align="left"),
urwid.Columns([urwid.Text(""),
(len(publish.label) + 4, publish),
(len(cancel.label) + 4, cancel)],
(len(publish.label) + 4, urwid.AttrMap(publish, "publish-normal", "publish-focus")),
(len(cancel.label) + 4, urwid.AttrMap(cancel, "cancel-normal", "cancel-focus"))],
dividechars=2)])
# Focus the text edit, which follows directly the conversation. This
......
......@@ -75,7 +75,11 @@ class Renderer:
entry("link-normal", "light blue,underline", "black"),
entry("link-focus", "light blue,underline", "white"),
entry("error-normal", "white,bold", "dark red"),
entry("error-focus", "dark red,bold", "white")
entry("error-focus", "dark red,bold", "white"),
entry("publish-normal", "white", "black"),
entry("publish-focus", "black", "white"),
entry("cancel-normal", "white", "black"),
entry("cancel-focus", "black", "white"),
]
self.unread_normal_map = {None: "unread-normal"}
......@@ -86,6 +90,8 @@ class Renderer:
"subject-normal": "subject-focus",
"link-normal": "link-focus",
"error-normal": "error-focus",
"publish-normal": "publish-focus",
"cancel-normal": "cancel-focus",
}
self._nicks = set()
for nick, colors in config.nick_colors:
......
......@@ -506,3 +506,43 @@ class DelegatingBoxAdapter(urwid.BoxAdapter):
canv = urwid.CompositeCanvas(canv)
return canv
class PushButton(urwid.Button):
"""
A button widget like `urwid.Button` but with its label in square brackets
rather than angled ones. Also emits the "click" signal when releasing the
button and not when pressing.
"""
button_left = urwid.Text("[")
button_right = urwid.Text("]")
def __init__(self, label, on_press=None, user_data=None):
# If we'd use super() instead, the overridden button_left/right
# class fields would not be used.
urwid.Button.__init__(self, label, on_press, user_data)
# hide the cursor
self._label._cursor_position = len(label)
self._last_mouse_event = None
self._last_mouse_button = None
def mouse_event(self, size, event, button, x, y, focus):
last_event = self._last_mouse_event
self._last_mouse_event = event
last_button = self._last_mouse_button
self._last_mouse_button = button
# We want the "click" signal to fire when the user released the left
# mouse button. This way, it is still possible to abort on press by
# draging the mouse away.
# http://urwid.org/manual/userinput.html#mouse-release-events says,
# that often there is no information on which mouse button is released.
# It will then be set to 0. But when we know some real button, it must
# be the left one.
if last_button == 1 and button in (0, 1) and last_event == "mouse press" and event == "mouse release":
self._emit("click")
return True
return False
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment