Created
October 9, 2014 19:26
-
-
Save pysmath/1881e45e6b486c4de48f to your computer and use it in GitHub Desktop.
Tabs.py
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
# Allows for tabbed editing within the pythonista editor | |
import ui | |
import editor | |
import sqlite3 as sql | |
import console | |
import os | |
from math import pi | |
open_tabs = {} | |
num_of_tabs = 0 | |
tab_width = 100 | |
tab_height = 45 | |
tab_y = 5 | |
count = 1 | |
tab_color = ['white'] | |
def add_new_button(name, new = False): | |
b = ui.Button(title = str(name)) | |
b.height = tab_height | |
b.width = tab_width | |
img = ui.Image('ionicons-close-circled-24') | |
b.image = img | |
b.border_width = 0.5 | |
b.corner_radius = 10 | |
if new == True: | |
b.background_color = 'orange' | |
else: | |
b.background_color = 'white' | |
b.border_color = 'grey' | |
b.tint_color = 'black' | |
b.action = open_url | |
b.transform = ui.Transform.rotation(pi/2) | |
global count | |
b.y = tab_width*count*1.05 - 25 | |
b.x = -10 | |
b.name = str(name) | |
close_title = labels[i] + '_close' | |
c = ui.Button() | |
c.width = 15 | |
c.height = 15 | |
c.x = 5 | |
c.y = tab_height/6 | |
c.corner_radius = c.height/2 | |
#c.border_width = 1 | |
c.image = ui.Image.named('ionicons-close-24') | |
c.action = close_button | |
b.add_subview(c) | |
view.add_subview(b) | |
count += 1 | |
def close_button(sender): | |
marker = sender.superview.y | |
tab = sender.superview | |
tab_name = sender.superview.title | |
view.remove_subview(tab) | |
def move(): | |
for i in range(len(view.subviews)): | |
if view.subviews[i].y > marker: | |
view.subviews[i].y -= tab_width*1.05 | |
ui.animate(move, duration = 0.3) | |
global count | |
count -=1 | |
conn = sql.connect('tabs.db') | |
conn.text_factory = str | |
c = conn.cursor() | |
c.execute('DELETE FROM files WHERE name = ?', (tab_name,)) | |
conn.commit() | |
# Create tab for current file | |
@ui.in_background | |
def add_file(sender): | |
current_path = str(editor.get_path()) | |
conn = sql.connect('tabs.db') | |
c = conn.cursor() | |
name = os.path.split(current_path)[1] | |
c.execute('''INSERT INTO files VALUES (?, ?)''', (name, current_path)) | |
conn.commit() | |
conn.close() | |
open_tabs.append(name) | |
add_new_button(name, new = True) | |
# Remove tab for current file | |
#def remove_file(sender): | |
# file_path = editor.get_path() | |
# conn = sql.connect('tabs.db') | |
# conn.text_factory = str | |
# c = conn.cursor() | |
# c.execute('SELECT name FROM files WHERE url = ?', (file_path,)) | |
# name = c.fetchone() | |
# c.execute('DELETE FROM files WHERE url = ?', (file_path,)) | |
# conn.commit() | |
# # Move tabs below the one being deleted up | |
# marker = view[name[0]].y | |
# view.remove_subview(view[name[0]]) | |
# def move(): | |
# for i in range(len(view.subviews)): | |
# if view.subviews[i].y > marker: | |
# view.subviews[i].y -= tab_width*1.05 | |
# ui.animate(move, duration = 0.3) | |
# global count | |
# count -=1 | |
# Open file when tab is pressed | |
def open_url(sender): | |
current_path = editor.get_path() | |
conn = sql.connect('tabs.db') | |
conn.text_factory = str | |
c = conn.cursor() | |
button_title = sender.title | |
c.execute('''select name from files where url = ?''', (current_path,)) | |
current_tab = c.fetchall() | |
if current_tab: | |
current_tab = current_tab[0][0] | |
view[current_tab].background_color = 'white' | |
c.execute('''SELECT url FROM files WHERE name = ?''', (button_title,)) | |
path = c.fetchone() | |
conn.close() | |
editor.open_file(path[0]) | |
sender.background_color = 'orange' | |
view = ui.load_view('Tabs') | |
add_button = view['add_button'] | |
remove = view['remove'] | |
# Create database and table on first run and make tabs for all files in database on start | |
first_time = False | |
current_path = editor.get_path() | |
if not os.path.isfile('tabs.db'): | |
first_time = True | |
conn = sql.connect('tabs.db') | |
conn.text_factory = str | |
c = conn.cursor() | |
if first_time == True: | |
c.execute('''CREATE TABLE files (name text, url text)''') | |
q = c.execute('''SELECT name FROM files''') | |
open_tabs = q.fetchall() | |
conn.close() | |
for i in range(len(open_tabs)): | |
add_new_button(open_tabs[i][0]) | |
view.present('sidebar', hide_title_bar=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment