Skip to content

Instantly share code, notes, and snippets.

@cas--
Created February 5, 2013 12:22
Show Gist options
  • Save cas--/4714136 to your computer and use it in GitHub Desktop.
Save cas--/4714136 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
#
# Copyright (C) 2012 Calum Lind <calumlind@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
from gi.repository import Gtk
import cairo
from gi.repository import AppIndicator3 as AppIndicator
import threading
NAME = "ind-test"
class IndTest:
def __init__(self):
self.indicator = AppIndicator.Indicator.new(
NAME,
NAME,
AppIndicator.IndicatorCategory.HARDWARE)
self.indicator.set_status(AppIndicator.IndicatorStatus.ACTIVE)
self.fn = 0
data = [
{'bars':6, 'color':0.90},
{'bars':13, 'color':0.90},
{'bars':2, 'color':0.90}
]
self.indicator.set_icon_full(self.create_icon(data),NAME)
self.menu = Gtk.Menu()
item = Gtk.MenuItem()
item.set_label("Exit")
item.connect("activate", self.handler_menu_exit)
item.show()
self.menu.append(item)
self.menu.show()
self.indicator.set_menu(self.menu)
def create_icon(self, data):
import cairo
icon_height = 22
icon_min_width = 22
outline_width = 11
outline_height = 17 # orig 14
outline_spacing = 3
bar_width = outline_width - 4
bar_height_max = outline_height - 4
bar_red = bar_height_max / 3
pad_left_right = 2
pad_top = 2
icon_width = len(data) * (outline_width + outline_spacing) + (pad_left_right*2) - outline_spacing
if icon_width < icon_min_width:
pad_left_right = ((icon_min_width - icon_width) / 2) + pad_left_right
icon_width = icon_min_width
def simpleroundedrec(context, x, y, w, h, r=1):
# A****B
# H C
# * *
# G D
# F****E
context.move_to(x+r, y + 0.5) # Move to A
context.rel_line_to(w-(2*r), 0) # Straight line to B
context.move_to(x+w,y+r) # Move to C
context.rel_line_to(0, h-(2*r)) # Straight line to D
context.rel_move_to(-r,r) # Move to E
context.rel_line_to(-w+(2*r),0) # Straight line to F
context.move_to(x+0.5,y+h-r) # Move to G
context.rel_line_to(0,-h+(2*r)) # Straight line to H
context.stroke ()
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, icon_width, icon_height)
ctx = cairo.Context (surface)
ctx.set_antialias(cairo.ANTIALIAS_NONE)
ctx.set_line_width (1)
offset = 0
for graph in data:
ctx.set_source_rgba (0, 0, 0, 0.3) # 30% Opaque black
simpleroundedrec(ctx, pad_left_right + offset, pad_top + 1, outline_width, outline_height)
ctx.set_source_rgb (0.87, 0.86, 0.82) # Solid color
simpleroundedrec(ctx, pad_left_right + offset, pad_top, outline_width, outline_height)
ctx.set_source_rgb (0.1, graph['color'], 0.8) # Solid color
#top left x, y, width, height
ctx.rectangle(
pad_left_right + 2 + offset,
outline_height + (pad_top - 2) - graph['bars'],
bar_width,
graph['bars']
)
ctx.fill()
if graph['bars'] == bar_height_max:
ctx.set_source_rgb (1, 0, 0) # Solid red
ctx.rectangle(
pad_left_right + 2 + offset,
outline_height + (pad_top - 2) - graph['bars'],
bar_width,
bar_red
)
ctx.fill()
offset = offset + outline_width + outline_spacing
filename_list = ['/tmp/test1.png', '/tmp/test2.png']
filename = filename_list[self.fn]
self.fn = not self.fn
surface.write_to_png(filename)
return filename
def calc_steps(self):
bars =1
perc = 100
line = ""
next = 3
step = 7
print '0 to %s' % (next - 1)
for x in range(perc + 1):
if x == next:
if line:
print line + str(x)
line = ''
next = x + 1
bars += 1
else:
line = '%s to ' % x
if x + step < perc-1:
next = x + step
print '%s to 100' % next
print bars + 1
def handler_menu_exit(self, event):
Gtk.main_quit()
def main(self):
Gtk.main()
if __name__ == "__main__":
indicator = IndTest()
indicator.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment