66 lines
2.1 KiB
Python
66 lines
2.1 KiB
Python
import gi
|
|
from gi.repository import Gtk, Gdk
|
|
|
|
class PromptView(Gtk.Box):
|
|
def __init__(self, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.prompt = Gtk.TextView(left_margin=12, top_margin=14,
|
|
right_margin=12, bottom_margin=14)
|
|
|
|
prompt_scroll = Gtk.ScrolledWindow(hexpand=True)
|
|
prompt_scroll.set_propagate_natural_height(True)
|
|
prompt_scroll.set_max_content_height(150)
|
|
prompt_scroll.set_child(self.prompt)
|
|
|
|
key_controller = Gtk.EventControllerKey()
|
|
key_controller.connect("key-pressed", self.on_key_pressed)
|
|
self.prompt.add_controller(key_controller)
|
|
|
|
self.prompt_button = Gtk.Button(icon_name="go-next-symbolic")
|
|
self.btn_handler = self.prompt_button.connect("clicked", self.on_send)
|
|
|
|
self.append (prompt_scroll)
|
|
self.append (self.prompt_button)
|
|
|
|
@property
|
|
def buffer(self):
|
|
return self.prompt.get_buffer()
|
|
|
|
def clear(self):
|
|
self.buffer.set_text("")
|
|
|
|
def get_text(self):
|
|
return self.buffer.get_text(self.buffer.get_start_iter(),
|
|
self.buffer.get_end_iter(), False).strip()
|
|
|
|
def hold(self, *args):
|
|
self.grab_focus()
|
|
self.prompt.set_sensitive(False)
|
|
self.prompt_button.set_icon_name("process-stop-symbolic")
|
|
self.prompt_button.disconnect(self.btn_handler)
|
|
self.btn_handler = self.prompt_button.connect("clicked", self.on_cancel)
|
|
|
|
def unhold(self, *args):
|
|
self.prompt.set_sensitive(True)
|
|
self.prompt.grab_focus()
|
|
self.prompt_button.set_icon_name("go-next-symbolic")
|
|
self.prompt_button.disconnect(self.btn_handler)
|
|
self.btn_handler = self.prompt_button.connect("clicked", self.on_send)
|
|
|
|
|
|
def on_key_pressed(self, _a, keyval, _b, state):
|
|
if keyval == Gdk.KEY_Return:
|
|
if state & Gdk.ModifierType.SHIFT_MASK:
|
|
return False
|
|
self.on_send()
|
|
return True
|
|
return False
|
|
|
|
def on_send(self, *args):
|
|
pass
|
|
|
|
def on_cancel(self, *args):
|
|
pass
|
|
|