107 lines
3.5 KiB
Python
Executable File
107 lines
3.5 KiB
Python
Executable File
#!/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
|
|
* Popup item click : display who clicked
|
|
* 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()
|
|
|
|
def run(self):
|
|
self.display_next_img()
|
|
self.runMainLoop()
|
|
|
|
def display_next_img(self):
|
|
filedata = urllib.request.urlopen('https://source.unsplash.com/random')
|
|
# Get redirected URL without parameters
|
|
url = filedata.url.split('?')[0]
|
|
filedata = urllib.request.urlopen(url + '?fit=max&width=500&height=500')
|
|
datatowrite = filedata.read()
|
|
with open('/tmp/cat2.jpg', 'wb') as f:
|
|
f.write(datatowrite)
|
|
widget = GenericMonitorTextWidget('#%d' % self.imgs_idx, 'color:purple')
|
|
url_widget = GenericMonitorTextWidget(url, 'color:white;font-weight:bold', signals={'on-click':'signal'}) # No name here
|
|
picture_widget = GenericMonitorPictureWidget('/tmp/cat2.jpg', name='NestedWidget', signals={'on-click':'signal'})
|
|
popup = GenericMonitorPopup([url_widget, picture_widget])
|
|
signals = {
|
|
'on-click':'toggle-popup',
|
|
# Could also use this behavior [bugged since GNOME 42]
|
|
# 'on-enter':'open-popup',
|
|
# 'on-leave':'close-popup',
|
|
'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):
|
|
return str(sender).endswith(self.item.getFullName())
|
|
|
|
def onClick(self, sender):
|
|
if not self._forMe(sender): return
|
|
print('Click from {}'.format(sender))
|
|
|
|
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()
|
|
|
|
def signal_handler(sig, frame):
|
|
picture.deleteGroups(['PicturePopup'])
|
|
sys.exit(0)
|
|
signal.signal(signal.SIGINT, signal_handler)
|
|
|
|
picture = PicturePopup()
|
|
picture.run()
|