Created
July 18, 2019 03:39
-
-
Save sizumita/53878510fa5a64c10c8e71979789efbf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from math import ceil, floor | |
from web import WebView | |
import appex | |
import ui | |
import clipboard | |
import os | |
max_frame = (0, 0, 359, 650) | |
COLS = 3 | |
ROWS = 2 | |
urls = { | |
'pythonista': 'pythonista3://', | |
'google': 'https://google.com/', | |
'tweetbot': 'tweetbot://', | |
} | |
SHORTCUTS = [ | |
{'title': 'Twitter', 'color': '#5e96ff', 'url': 'tweetbot'}, | |
{'title': 'Qiita', 'color': '#5e96ff', 'View': 'qiita'}, | |
{'title': 'Discord', 'color': '#5e96ff', 'url': None}, | |
{'title': 'Google', 'color': '#5e96ff', 'url': 'google'}, | |
{'title': 'Pythonista', 'color': '#5e96ff', 'url': 'pythonista'}, | |
{'title': 'file', 'url': 'file'} | |
] | |
WEB_COLS = 5 | |
WEB_ROWS = 1 | |
WEB_BUTTONS = [ | |
{'title': 'Menu'}, | |
{'title': 'Safari'}, | |
{'title': 'back'}, | |
{'title': 'go'}, | |
{'title': 'Set URL'}, | |
] | |
class MainView(ui.View): | |
mode = 'menu' | |
def __init__(self): | |
row_height = 110 / ROWS | |
super().__init__(frame=(0, 0, 300,650) | |
) | |
self.buttons = [] | |
self.webview = None | |
self.set_menu_views() | |
def set_menu_views(self): | |
for btn in self.buttons: | |
self.remove_subview(btn) | |
self.buttons.clear() | |
self.mode = 'menu' | |
if self.webview: | |
self.remove_subview(self.webview) | |
for s in SHORTCUTS: | |
btn = ui.Button( | |
title=' ' + s['title'], | |
#image=ui.Image(s.get('icon', None)), | |
name=s.get('View', s.get('url', None)), | |
action=self.button_action, | |
bg_color=s.get('color', '#55bcff'), | |
tint_color='#fff', | |
corner_radius=9 | |
) | |
self.add_subview(btn) | |
self.buttons.append(btn) | |
def set_web_views(self, sender, url='https://qiita.com/'): | |
for btn in self.buttons: | |
self.remove_subview(btn) | |
self.buttons.clear() | |
self.mode = 'web' | |
for s in WEB_BUTTONS: | |
btn = ui.Button(title=' ' + s['title'], | |
name=s['title'], | |
bg_color=s.get('color', '#55bcff'), | |
tint_color='#fff', | |
corner_radius=9, | |
action=self.button_action_web | |
) | |
if s.get('icon', False): | |
btn.image = ui.Image(s['icon']) | |
self.add_subview(btn) | |
self.buttons.append(btn) | |
h = floor(self.height / WEB_ROWS) if self.height <= 130 else floor(110 / WEB_ROWS) | |
self.webview = ui.WebView(frame=(0, h+5, 300, 500)) | |
if clipboard.get().startswith('http') and not url: | |
url = clipboard.get() | |
self.webview.load_url(url) | |
self.add_subview(self.webview) | |
def layout(self): | |
if self.mode == 'menu': | |
bw = self.width / COLS | |
bh = floor(self.height / ROWS) if self.height <= 130 else floor(110 / ROWS) | |
for i, btn in enumerate(self.buttons): | |
btn.frame = ui.Rect(i % COLS * bw, i // COLS * bh, bw, bh).inset(2, 2) | |
btn.alpha = 1 if btn.frame.max_y < self.height else 0 | |
elif self.mode == 'web': | |
bw = self.width / WEB_COLS | |
bh = floor(self.height / WEB_ROWS) if self.height <= 130 else floor(110 / WEB_ROWS) | |
for i, btn in enumerate(self.buttons): | |
btn.frame = ui.Rect(i % WEB_COLS * bw, i // WEB_COLS * bh, bw, bh/2).inset(2, 2) | |
btn.alpha = 1 if btn.frame.max_y < self.height else 0 | |
def button_action(self, sender): | |
if sender.name in urls.keys(): | |
import webbrowser | |
webbrowser.open(urls[sender.name]) | |
elif sender.name == 'qiita': | |
self.set_web_views(sender) | |
def button_action_web(self, sender): | |
if sender.name == 'Safari': | |
url = self.webview.eval_js('window.location.href') | |
import webbrowser | |
webbrowser.open(url) | |
elif sender.name == 'Menu': | |
pass | |
elif sender.name == 'back': | |
self.webview.go_back() | |
elif sender.name == 'go': | |
self.webview.go_forward() | |
elif sender.name == 'Set URL': | |
self.webview.load_url(clipboard.get()) | |
def main(): | |
widget_name = __file__ + str(os.stat(__file__).st_mtime) | |
v = appex.get_widget_view() | |
# Optimization: Don't create a new view if the widget already shows the launcher. | |
if v is None or v.name != widget_name: | |
v = MainView() | |
v.name = widget_name | |
appex.set_widget_view(v) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment