GnomeShellGenericMonitor/examples/picture.py

104 lines
3.3 KiB
Python
Raw Permalink Normal View History

2020-11-18 16:20:48 +01:00
#!/usr/bin/env python3
#
# 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, see <https://www.gnu.org/licenses/>.
#
'''
Display random picture from unsplash.com in a popup
* Click : open/close popup
2021-01-18 15:53:41 +01:00
* Popup item click : display who clicked
2020-11-18 16:20:48 +01:00
* ScrollUp/ScrollDown/Double click : display next picture
* Right click : exit
'''
import urllib.request
import sys
import signal
from genericmonitor import *
class PicturePopup(GenericMonitor):
def __init__(self):
self.scrolling = False
self.item = None
self.imgs_idx = 0
self.setupMonitor()
2020-11-18 16:32:29 +01:00
def run(self):
2020-11-18 16:20:48 +01:00
self.display_next_img()
self.runMainLoop()
2020-11-18 16:32:29 +01:00
2020-11-18 16:20:48 +01:00
def display_next_img(self):
2023-11-26 09:57:02 +01:00
filedata = urllib.request.urlopen('https://picsum.photos/500/500')
2020-11-18 16:20:48 +01:00
datatowrite = filedata.read()
with open('/tmp/cat2.jpg', 'wb') as f:
f.write(datatowrite)
widget = GenericMonitorTextWidget('#%d' % self.imgs_idx, 'color:purple')
2023-11-26 09:57:02 +01:00
url_widget = GenericMonitorTextWidget('random_pic', 'color:white;font-weight:bold', signals={'on-click':'signal'}) # No name here
2021-01-18 15:53:41 +01:00
picture_widget = GenericMonitorPictureWidget('/tmp/cat2.jpg', name='NestedWidget', signals={'on-click':'signal'})
2020-11-18 16:20:48 +01:00
popup = GenericMonitorPopup([url_widget, picture_widget])
signals = {
'on-click':'toggle-popup',
2022-10-27 17:54:37 +02:00
# Could also use this behavior [bugged since GNOME 42]
# 'on-enter':'open-popup',
# 'on-leave':'close-popup',
2020-11-18 16:20:48 +01:00
'on-dblclick':'signal',
'on-rightclick':'signal',
'on-scroll':'signal',
}
self.item = GenericMonitorItem('picturepopup', [widget], signals, popup)
group = GenericMonitorGroup('PicturePopup', [self.item])
self.notify(group)
self.imgs_idx += 1
def _forMe(self, sender):
2021-01-18 15:53:41 +01:00
return str(sender).endswith(self.item.getFullName())
def onClick(self, sender):
if not self._forMe(sender): return
print('Click from {}'.format(sender))
2020-11-18 16:20:48 +01:00
def _onScroll(self, sender):
if not self._forMe(sender): return
if self.scrolling: return
self.scrolling = True
self.display_next_img()
self.scrolling = False
def onScrollUp(self, sender):
self._onScroll(sender)
def onScrollDown(self, sender):
self._onScroll(sender)
def onDblClick(self, sender):
self.onScrollUp(sender)
def onRightClick(self, sender):
if not self._forMe(sender): return
if self.item:
self.deleteItems([self.item.getFullName()])
self.stopMainLoop()
2020-11-18 16:32:29 +01:00
def signal_handler(sig, frame):
picture.deleteGroups(['PicturePopup'])
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
2020-11-18 16:20:48 +01:00
picture = PicturePopup()
2020-11-18 16:32:29 +01:00
picture.run()