def init_size(self): # initialize with correct size when landscape orientation = ui.WebView(frame=(0,0,100,200)).eval_js('window.orientation') if orientation in (-90, 90): self.frame = (0, 0, self.height, self.width)
def did_load(self): self.init_buttons() self.init_webbrowser() self.init_addressbar() self.init_size() self.flex = 'WH' self.bookmarks = self.load_bookmarks() self.history = self.load_history() self.addressbar_is_editing = False self.webpage_has_loaded = False self.favourite_images = {True :ui.Image.named('ionicons-ios7-star-32'), False:ui.Image.named('ionicons-ios7-star-outline-32')}
def save_history(self, filename=filename_history): with open(filename, 'w') as f: url = self.get_url() if url in self.history: self.history.remove(url) self.history.append(url) f.seek(0) pickle.dump(self.history, f) def clear_history(self, sender, filename=filename_history): with open(filename, 'w') as f: self.history = [] f.seek(0) pickle.dump(self.history, f) sender.superview.superview['history'].data_source.items = self.history sender.superview.superview['history'].reload()
def save_bookmark(self, filename=filename_bookmarks): with open(filename, 'w') as f: url = self.get_url() title = self.get_title() or self.parse_url(url) self.bookmarks[title] = url f.seek(0) json.dump(self.bookmarks, f, indent=4) self['controlpanel']['favourite'].image = self.favourite_images[True]
def remove_bookmark(self, title=None, filename=filename_bookmarks): with open(filename, 'w') as f: title = title or self.get_title() del self.bookmarks[title] f.seek(0) json.dump(self.bookmarks, f, indent=4) self['controlpanel']['favourite'].image = self.favourite_images[False]
def popup_menu(self): popup = ui.View(name='menu', frame=(0, 0, 320, 500)) toolbar = ui.View(frame=(-5, 0, 330, 100), name='toolbar') toolbar.border_width = 0.5 toolbar.border_color = '#B2B2B2' label = ui.Label() label.text = 'Bookmarks' label.alignment = ui.ALIGN_CENTER label.frame = (0, 0, 320, 50) label.name = 'title' segment_ctrl = ui.SegmentedControl(name='segctrl') segment_ctrl.segments = ['Bookmarks', 'History'] segment_ctrl.width = 170 segment_ctrl.center = popup.center segment_ctrl.y = label.height segment_ctrl.selected_index = 0 segment_ctrl.action = self.bookmarks_or_history button = ui.Button() button.frame = (segment_ctrl.x*3.5, segment_ctrl.y, 60, 30) button.font = ('<system>', 15) button.title= 'Clear' button.name = 'clear' button.action = self.clear_history button.hidden = True toolbar.add_subview(label) toolbar.add_subview(segment_ctrl) toolbar.add_subview(button) popup.add_subview(toolbar) data_source = ui.ListDataSource(sorted(self.bookmarks.keys())) popup.add_subview(self.list_bookmarks_and_history(data_source, width=320,height=toolbar.superview.height-toolbar.height, y=toolbar.height, name='bookmarks')) x, y = self['controlpanel']['bookmarks'].center popup.present('popover', popover_location=(x, y), hide_title_bar=True)
def bookmarks_or_history(self, sender): toolbar = sender.superview if sender.selected_index == 0: toolbar['clear'].hidden = True toolbar['title'].text = 'Bookmarks' data_source = ui.ListDataSource(sorted(self.bookmarks.keys())) tv = self.list_bookmarks_and_history(data_source, width=320, height=toolbar.superview.height-toolbar.height, y=toolbar.height, name='bookmarks') toolbar.superview.remove_subview(toolbar.superview['history']) else: toolbar['clear'].hidden = False toolbar['title'].text = 'History' data_source = ui.ListDataSource(self.history[::-1]) tv = self.list_bookmarks_and_history(data_source, width=320, height=toolbar.superview.height-toolbar.height, y=toolbar.height, name='history') toolbar.superview['bookmarks'].hidden=True toolbar.superview.remove_subview(toolbar.superview['bookmarks']) sender.superview.superview.add_subview(tv)
def list_bookmarks_and_history(self, data_source, **kwargs): tv = ui.TableView() tv.data_source = data_source tv.delegate = self for k, v in kwargs.items(): setattr(tv, k, v) return tv
def show_more_menu(self): popup = ui.TableView() popup.width = 250 popup.height = 500 popup.name = 'More' popup.data_source = popup.delegate = self button = self['controlpanel']['more'] popup.present('popover', popover_location=(button.x, button.y+button.height))
def button_tapped(self, sender): if sender.name == 'favourite': if self.get_url() in self.bookmarks.values(): self.remove_bookmark() else: self.save_bookmark() elif sender.name == 'bookmarks': self.popup_menu() elif sender.name == 'more': self.show_more_menu() else: eval("self['webview'].{}()".format(sender.name))
def tableview_number_of_rows(self, tableview, section): if tableview.name == 'Bookmarks': return len(self.bookmarks) elif tableview.name == 'More': return 1
def tableview_cell_for_row(self, tableview, section, row): if tableview.name == 'Bookmarks': cell = ui.TableViewCell() cell.text_label.text = sorted(self.bookmarks.keys())[row] cell.image_view.image = ui.Image.named('ionicons-ios7-bookmarks-outline-32') cell.image_view.tint_color = '#66CCFF' return cell elif tableview.name == 'More': cell = ui.TableViewCell() cell.text_label.text = 'Settings' cell.image_view.image = ui.Image.named('ionicons-wrench-32') return cell
@ui.in_background def tableview_did_select(self, tableview, section, row): if tableview.name == 'bookmarks': url = self.bookmarks[sorted(self.bookmarks.keys())[row]] self.load_url(url) tableview.superview.close() elif tableview.name == 'history': url = tableview.data_source.items[row] tableview.superview.close() self.load_url(url) elif tableview.name == 'More': tableview.close() console.hud_alert('No settings yet...', 'error', 1)
def tableview_can_delete(self, tableview, section, row): return True
def tableview_delete(self, tableview, section, row): item = sorted(self.bookmarks.keys())[row] self.remove_bookmark(item) tableview.reload()
def textfield_did_begin_editing(self, textfield): self.addressbar_is_editing = True self.set_url() self['controlpanel']['reload'].hidden = True
def textfield_did_end_editing(self, textfield): self.addressbar_is_editing = False self['controlpanel']['reload'].hidden = False self.set_url()
def textfield_should_return(self, textfield): url = self['controlpanel']['addressbar'].text self.load_url(url) textfield.end_editing() return True
def webview_did_start_load(self, webview): self.webpage_has_loaded = False
def webview_did_finish_load(self, webview): if not self.addressbar_is_editing: self.set_url() self.webpage_has_loaded = True page_is_bookmarked = unicode(self.get_url()) in self.bookmarks.values() self['controlpanel']['favourite'].image = self.favourite_images[page_is_bookmarked] self.save_history()
python类WebView()的实例源码
def get_view(theme_manager):
wv = ui.WebView()
bh = buttonHandler(wv, theme_manager.currentTheme.tintColour)
wvd = webViewDelegate(theme_manager.currentTheme.invertWebView, bh)
wv.delegate = wvd
wv.right_button_items = bh.getReloadButtons()
return wv
def main():
title = os.path.split(editor.get_path())[1]
html = editor.get_text()
webview = ui.WebView(title)
webview.load_html(html)
webview.present()
def load_html(self, html):
wv = ui.WebView('Markdown Preview')
wv.scales_page_to_fit = True
wv.load_html(html)
w, h = scene.get_screen_size()
wv.width = w
wv.height = h-60
self.add_subview(wv)
def __init__(self):
'''Children must call RootView.__init__(self), in order to set up hidden webview!'''
self.__w=ui.WebView(frame=(1,1,1,1))
self.add_subview(self.__w)
def __init__(self):
'''Children must call RootView.__init__(self), in order to set up hidden webview!'''
self.__w=ui.WebView(frame=(1,1,1,1))
self.add_subview(self.__w)
def diff_working(repo,file,src=source.PATH):
store=repo.object_store
index=repo.open_index()
tree=store[store[repo.head()].tree]
parent_tree=store[store[store[repo.head()].parents[0]].tree]
tree_ver=store[tree.lookup_path(store.peel_sha,file)[1]].data
local_ver=open(os.path.join(repo.path,file)).read()
h=HtmlDiff(wrapcolumn=70,tabsize=4)
if src==source.PATH:
f=h.make_file(tree_ver.splitlines(),local_ver.splitlines(), file, 'last commit:'+repo.head())
elif src==source.INDEX:
index_ver=store[index._byname[file].sha].data
f=h.make_file(tree_ver.splitlines(),index_ver.splitlines(),file+' staged change', 'last commit'+repo.head())
else:
parent_tree_ver=store[parent_tree.lookup_path(store.peel_sha,file)[1]].data
f=h.make_file(parent_tree_ver.splitlines(),tree_ver.splitlines(), file+' HEAD','HEAD^1')
return f
#f=diff_working(porcelain.open_repo('.'),'gitui.py')
#w=ui.WebView()
#w.load_html(f)
#w.present()
def __init__(self, wikiname, basewikiurl, wikiurl):
self.modulepath = os.path.dirname(os.path.abspath(__file__))
os.chdir(self.modulepath)
self.wikidir = os.path.expanduser('~/.mw-' + wikiname)
if not os.path.isdir(self.wikidir):
os.mkdir(self.wikidir)
self.webdelegate = WebViewDelegate(self)
self.SearchTableViewDelegate = SearchTableViewDelegate
if not wikiurl.endswith('/'):
wikiurl += '/'
# Create URLs
assert not wikiurl.startswith('http'), 'must be end of wiki url'
if basewikiurl.endswith('/'):
basewikiurl = basewikiurl[:-1]
self.basewikiurl = basewikiurl
self.wikiurl = self.basewikiurl + wikiurl
self.searchurl = self.wikiurl + 'Special:Search?search='
self.history = []
self.histIndex = 0
self.back = False
self.closed = False
if len(sys.argv) > 2:
self.args = True
else:
self.args = False
# Create WebView
self.webview = ui.WebView()
self.mainSource = ''
self.webview.delegate = WebViewDelegate
self.loadPage(self.wikiurl)
self.searchButton = ui.ButtonItem(image=ui.Image.named(
'iob:search_24'), action=self.searchTapped)
self.reloadButton = ui.ButtonItem(image=ui.Image.named(
'iob:refresh_24'), action=self.reloadTapped)
self.backButton = ui.ButtonItem(image=ui.Image.named(
'iob:arrow_left_c_24'), action=self.backTapped)
self.fwdButton = ui.ButtonItem(image=ui.Image.named(
'iob:arrow_right_c_24'), action=self.fwdTapped)
self.homeButton = ui.ButtonItem(image=ui.Image.named('iob:home_24'),
action=self.home)
self.shareButton = ui.ButtonItem(image=ui.Image.named(
'iob:ios7_upload_outline_32'), action=self.share)
self.safariButton = ui.ButtonItem(image=ui.Image.named(
'iob:compass_24'), action=self.safari)
self.webview.right_button_items = [self.searchButton,
self.reloadButton,
self.fwdButton,
self.backButton,
self.homeButton]
self.webview.left_button_items = [self.shareButton, self.safariButton]
self.webview.present('fullscreen', animated=True)
self.previousSearch = ''
if len(sys.argv) > 1:
self.search(sys.argv[1])
closeThread = threading.Thread(target=self.waitForClose)
closeThread.start()
def __init__(self, frame = None, flex = None, background_color = None, name = None, accessory_keys = True, extras = [], css = None):
if frame: self.frame = frame
if flex: self.flex = flex
if background_color: self.background_color = background_color
if name: self.name = name
self.extras = extras
self.css = css or self.default_css
self.proxy_delegate = None
self.enable_links = True
self.editing = False
self.margins = (10, 10, 10, 10)
self.link_prefix = 'pythonista-markdownview:relay?content='
self.debug_prefix = 'pythonista-markdownview:debug?content='
self.init_postfix = '#pythonista-markdownview-initialize'
self.in_doc_prefix = ''
self.to_add_to_beginning = ('', -1)
self.backpanel = ui.View()
self.add_subview(self.backpanel)
# Web fragment is used to find the right scroll position when moving from editing to viewing
self.web_fragment = ui.WebView()
self.web_fragment.hidden = True
self.web_fragment.delegate = MarkdownView.ScrollLoadDelegate()
self.add_subview(self.web_fragment)
self.markup = ui.TextView()
self.add_subview(self.markup)
self.markup.font = ('<system>', 12)
self.web = ui.WebView()
self.web.scales_page_to_fit = False
self.web.content_mode = ui.CONTENT_TOP_LEFT
self.add_subview(self.web)
self.web.delegate = self
self.markup.delegate = self
self.markup.text = ''
self.update_html()
self.markup.bounces = False
# Ghosts are used to determine preferred size
self.markup_ghost = ui.TextView()
self.markup_ghost.hidden = True
#self.add_subview(self.markup_ghost)
self.web_ghost = ui.WebView()
self.web_ghost.hidden = True
#self.add_subview(self.web_ghost)
if accessory_keys:
self.create_accessory_toolbar()
def webview_should_start_load(self, webview, url, nav_type):
# Click, should start edit in markdown
if url.startswith(self.link_prefix):
left_side = unquote(url.replace(self.link_prefix, ''))
self.start_editing(left_side.split())
#webview.stop()
return False
# Debug message from web page, print to console
elif url.startswith(self.debug_prefix):
debug_text = unquote(url.replace(self.debug_prefix, ''))
print debug_text
return False
# Loaded by the web view at start, allow
elif url.startswith('about:blank'):
return True
# Custom WebView initialization message
# Used to check if in-doc links starting with '#'
# have extra stuff in front
elif url.endswith(self.init_postfix):
self.in_doc_prefix = url[:len(url)-len(self.init_postfix)]
self.web.hidden = False
return False
# If link starts with the extra stuff detected
# at initialization, remove the extra
if url.startswith(self.in_doc_prefix):
url = url[len(self.in_doc_prefix):]
# Check for custom link handling
if self.can_call('webview_should_start_load'):
return self.proxy_delegate.webview_should_start_load(webview, url, nav_type)
# Handle in-doc links within the page
elif url.startswith('#'):
if self.can_call('webview_should_load_internal_link'):
return self.proxy_delegate.webview_should_load_internal_link(webview, url)
return True
# Open 'http(s)' links in Safari
# 'file' in built-in browser
# Others like 'twitter' as OS decides
else:
if self.can_call('webview_should_load_external_link'):
return self.proxy_delegate.webview_should_load_external_link(webview, url)
if url.startswith('http:') or url.startswith('https:'):
url = 'safari-' + url
webbrowser.open(url)
return False