Skip to content
Open
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
53 changes: 27 additions & 26 deletions indicator-chars.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python
#!/usr/bin/python3
# -*- coding: utf-8 -*-
#
# Very simple chars indicator.
Expand Down Expand Up @@ -32,11 +32,13 @@

import os
import re
import gtk
import gio
import gi
import signal
import subprocess
import appindicator

gi.require_version('Gtk', '3.0')
gi.require_version('AppIndicator3', '0.1')
from gi.repository import Gtk, Gdk, Gio, AppIndicator3, GObject

APP_NAME = 'indicator-chars'
APP_VERSION = '0.2'
Expand All @@ -49,34 +51,33 @@ class IndicatorChars:
description_pattern = re.compile(r' *(\([^)]+\)) *')

def __init__(self):
self.ind = appindicator.Indicator(
self.ind = AppIndicator3.Indicator.new(
"Chars", os.path.join(self.SCRIPT_DIR, 'light16x16.png'),
appindicator.CATEGORY_APPLICATION_STATUS)
self.ind.set_status(appindicator.STATUS_ACTIVE)
AppIndicator3.IndicatorCategory.APPLICATION_STATUS)
self.ind.set_status(AppIndicator3.IndicatorStatus.ACTIVE)

self.update_menu()

def create_menu_item(self, label):
item = gtk.MenuItem()
item = Gtk.MenuItem()
item.set_label(label)
return item

def on_chars_changed(self, filemonitor, file, other_file, event_type):
if event_type == gio.FILE_MONITOR_EVENT_CHANGES_DONE_HINT:
print 'Characters changed, updating menu...'
if event_type == Gio.FileMonitorEvent.CHANGES_DONE_HINT:
print('Characters changed, updating menu...')
self.update_menu()

def update_menu(self, widget = None, data = None):
try:
charDef = open(self.CHARS_PATH).readlines()
except IOError:
charDef = []

# Create menu
menu = gtk.Menu()
menu = Gtk.Menu()

for charLine in charDef:
charLine = unicode(charLine)
charLine = charLine.strip()
submenu_match = self.submenu_title_pattern.match(charLine)
if submenu_match:
Expand All @@ -87,7 +88,7 @@ def update_menu(self, widget = None, data = None):
submenu_title = ''.join(
self.description_pattern.split(charLine)[::2])
parentItem = self.create_menu_item(submenu_title)
subMenu = gtk.Menu()
subMenu = Gtk.Menu()
while charLine:
char = charLine[0]
charLine = charLine[1:]
Expand All @@ -104,7 +105,7 @@ def update_menu(self, widget = None, data = None):
parentItem.set_submenu(subMenu)
menu.append(parentItem)

menu.append(gtk.SeparatorMenuItem())
menu.append(Gtk.SeparatorMenuItem())
quit_item = self.create_menu_item('Quit')
quit_item.connect("activate", self.on_quit)
menu.append(quit_item)
Expand All @@ -114,24 +115,24 @@ def update_menu(self, widget = None, data = None):
menu.show_all()

def on_char_click(self, widget, char):
cb = gtk.Clipboard(selection="PRIMARY")
cb.set_text(char)
cb = Gtk.Clipboard.get(Gdk.SELECTION_PRIMARY)
cb.set_text(char, -1)

def on_quit(self, widget):
gtk.main_quit()
Gtk.main_quit()


if __name__ == "__main__":
# Catch CTRL-C
signal.signal(signal.SIGINT, lambda signal, frame: gtk.main_quit())
signal.signal(signal.SIGINT, lambda signal, frame: Gtk.main_quit())

# Run the indicator
i = IndicatorChars()

# Monitor bookmarks changes
file = gio.File(i.CHARS_PATH)
monitor = file.monitor_file()
monitor.connect("changed", i.on_chars_changed)
file = Gio.File.new_for_path(i.CHARS_PATH)
monitor = file.monitor_file(Gio.FileMonitorFlags.NONE, None)
monitor.connect("changed", i.on_chars_changed)

# Main gtk loop
gtk.main()
Gtk.main()