Forum Archive

Help with text field delegate

techteej

I'm not sure how to integrate the text field delegate into my source code. Here is what I have so far.

# coding: utf-8
from os import listdir, getcwd
from editor import open_file
from console import hud_alert
import ui

# search + opening files do not work yet

class MyTextFieldDelegate (object):
        def textfield_did_end_editing(self, textfield):
                query = v['searchbox'].text
                try:
                    if query in dir_items:
                        tv1 = v['file-table']
                        tv1.data_source = ui.ListDataSource([query])
                    else:
                        tv1 = v['file-table']
                        tv1.data_source = None
                except IndexError:
                    pass

def table_data():
    global dir_items
    dir_items = listdir(getcwd())
    lst = ui.ListDataSource(dir_items)
    tv1 = v['file-table']
    tv1.data_source = lst
    tv1.reload()

def search_files(sender):
    global dir_items
    table_data()
    MyTextFieldDelegate()

def select(sender):
    v.close()
    #open_file("~/Notepad" + [sender.selected_row])
    print(sender, sender.selected_row, sender.items)

def make_file():
    global file_name, created_file
    with open(file_name, 'w') as out_file:
            out_file.write(created_file)
    table_data()
    hud_alert('File successfully created!', 'success', 1.0)

def check(sender):
    global file_name, created_file
    file_name = v['namefield'].text + '.txt'
    if file_name == '.txt':
        file_name = 'Untitled' + '.txt'
    else: 
        pass
    created_file = v['textview1'].text
    if created_file == '':
        hud_alert('No text entered.', 'error', 1.0)
    else:   
        make_file()

v = ui.load_view('notepad')
table_data()
v['searchbox'].clear_button_mode = 'while_editing'
v.present('full_screen')
JonB

Why do you think you need a delegate? As opposed to just an action?
If you are only implementing textfield_did_end_editing, this is pretty much the same as action.

If you are sure you want to use a delegate, you'd set v['searchbox'].delegate = MyTextFieldDelegate() after you initialize your view, or in the ui designer ( I forget if this is an option). Also note that you don't have to use global variables to get the search field... either the action's sender already contains the TextField, or the delegate method's textfield.

Are you trying to implement some sort of wild card search box? If so, I'm not sure this is doing what you think or want it to do. You might start simpler, and see if you can generate a list of files that match some pattern, without using ui. Look at the glob module.

as an aside:
It is not necessary to create a whole new ListDataSource object every time you want to update its contents ... just create it one time when you initialize your view, attach it to your tv1.data_source, and then whenever you need to update, you'd update your tv1.data_source.item.

techteej

I put this in a text field action (was previously unaware that this was possible) but now the function doesn't work. Worked fine as a button action, but not really what I was looking for design wise.