Skip to content

Instantly share code, notes, and snippets.

@pysmath
Created Oct 9, 2014
Embed
What would you like to do?
Tabs
# 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):
b = ui.Button(title = str(name))
b.height = tab_height
b.width = tab_width
b.border_width = 0.5
b.corner_radius = 10
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 + 10
b.x = -10
b.name = str(name)
view.add_subview(b)
count += 1
# 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 = console.input_alert('File Name')
c.execute('''INSERT INTO files VALUES (?, ?)''', (name, current_path))
conn.commit()
conn.close()
open_tabs.append(name)
add_new_button(name)
# 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')
[{"class":"View","attributes":{"background_color":"RGBA(0.928571,0.928571,0.928571,1.000000)","tint_color":"RGBA(0.000000,0.478000,1.000000,1.000000)","enabled":true,"border_width":0.5,"border_color":"RGBA(0.785714,0.785714,0.785714,1.000000)","flex":""},"frame":"{{0, 0}, {75, 500}}","nodes":[{"class":"Button","attributes":{"background_color":"RGBA(1.000000,1.000000,1.000000,1.000000)","image_name":"ionicons-ios7-plus-outline-24","border_color":"RGBA(0.714286,0.714286,0.714286,1.000000)","font_size":15,"title":" Add","enabled":true,"flex":"","tint_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","action":"add_file","font_bold":true,"name":"add_button","corner_radius":0,"border_width":1,"uuid":"5EB338B8-2E61-4C9A-A8C3-FF6EAEFCDE98"},"frame":"{{-1, 42}, {77, 32}}","nodes":[]},{"class":"Button","attributes":{"background_color":"RGBA(1.000000,1.000000,1.000000,1.000000)","image_name":"ionicons-ios7-minus-outline-24","border_color":"RGBA(0.714286,0.714286,0.714286,1.000000)","font_size":15,"title":"Delete","enabled":true,"flex":"","tint_color":"RGBA(0.000000,0.000000,0.000000,1.000000)","action":"remove_file","font_bold":true,"name":"remove","corner_radius":0,"border_width":1,"uuid":"B7C00527-A6C3-48B9-94E0-707AC2237998"},"frame":"{{-1, 73.5}, {77, 32}}","nodes":[]}]}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment