omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    The performance of button.name is kind of slow?

    Pythonista
    3
    61
    20812
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • ?
      A Former User @cvp last edited by A Former User

      @cvp Sure. https://github.com/yjqiang/yj_ebook_reader/blob/master/index_viewer/index_viewer.py#L109 I use self.cur_offset and the new offset to check whether users are scrolling down or up.
      If scrolling down(the scrollbar is going down), I use https://github.com/yjqiang/yj_ebook_reader/blob/master/index_viewer/index_viewer.py#L122 to handle this. If the lastest part of the subviews is done (https://github.com/yjqiang/yj_ebook_reader/blob/master/index_viewer/index_viewer.py#L126), I will move the first part of the subviews to the end of them(https://github.com/yjqiang/yj_ebook_reader/blob/master/index_viewer/index_viewer.py#L137-L141)

      cvp 2 Replies Last reply Reply Quote 0
      • cvp
        cvp @Guest last edited by

        @lpl if I comment all the lines where you change .title, you are right, no blank...

        1 Reply Last reply Reply Quote 0
        • cvp
          cvp @Guest last edited by

          @lpl only line 137 needs to be commented and white disappears...

          ? 1 Reply Last reply Reply Quote 0
          • ?
            A Former User @cvp last edited by A Former User

            @cvp But the function is broken… If you delete L137

            cvp 1 Reply Last reply Reply Quote 0
            • cvp
              cvp @Guest last edited by

              @lpl I understand, it is only to show from which line the problem comes. It is not yet a solution...

              ? 1 Reply Last reply Reply Quote 0
              • ?
                A Former User @cvp last edited by

                @cvp So the performance of button.title is not good.

                cvp 2 Replies Last reply Reply Quote 0
                • cvp
                  cvp @Guest last edited by

                  @lpl It seems, yes. Thus you were right from the beginning 😀

                  1 Reply Last reply Reply Quote 0
                  • ?
                    A Former User last edited by

                    @omz Can you check this? And thank you @cvp . You are so patient.

                    1 Reply Last reply Reply Quote 0
                    • cvp
                      cvp @Guest last edited by

                      @lpl Really not sure that could help, but you could try to add a label as subview of the button, with transparent background so the button stays clickable, and set the label.text instead of the button.title

                      ? 1 Reply Last reply Reply Quote 0
                      • ?
                        A Former User @cvp last edited by

                        @cvp said:

                        @lpl Really not sure that could help, but you could try to add a label as subview of the button, with transparent background so the button stays clickable, and set the label.text instead of the button.title

                        I'll have a try. And thx again.

                        cvp 1 Reply Last reply Reply Quote 0
                        • cvp
                          cvp @Guest last edited by

                          @lpl The problem is not for @omz. It seems it comes from animation of UIButton when its title changes

                          ? 1 Reply Last reply Reply Quote 0
                          • ?
                            A Former User @cvp last edited by

                            @cvp said:

                            @lpl The problem is not for @omz. It seems it comes from animation of UIButton when its title changes

                            Maybe he should give an api to control this? I don't know. But I want to hear his advice.

                            mikael 1 Reply Last reply Reply Quote 0
                            • mikael
                              mikael @Guest last edited by

                              @lpl, do you have a compelling reason to use a button instead of a custom View with touch_ended? Custom view will also let you do whatever you want with images and labels.

                              cvp ? 2 Replies Last reply Reply Quote 0
                              • cvp
                                cvp @mikael last edited by

                                @mikael I agree, this not very different than a button without title and a label as subview....

                                1 Reply Last reply Reply Quote 0
                                • ?
                                  A Former User @mikael last edited by

                                  @mikael I didn't try a custom View before. You mean I can make something like Button or Label? And push these things into the ScrollView?

                                  cvp mikael 2 Replies Last reply Reply Quote 0
                                  • cvp
                                    cvp @Guest last edited by ccc

                                    @lpl I am estonished (of) my-self but after hours of search and tests,, I have been able to solve your problem with Objective-c.
                                    That uses the fact that ui.Button is not a real UIButton but has it as subview, and also uses a block to execute code outside the animation process of an UIButton...
                                    Replace index_viewer.py by

                                    from collections import deque
                                    import ui
                                    import console
                                    from objc_util import *
                                    
                                    UIButton = ObjCClass('UIButton') 
                                    
                                    class IndexViewer:
                                        ITEM_H = 32
                                        LEN_LINE = 19
                                        LOADING = 'LOADING...'
                                            
                                        def __init__(self, parent):
                                            reader_view = ui.load_view('index_viewer/index')
                                            scrollview = reader_view['scrollview']
                                            
                                            for i in range(19):
                                                button = reader_view[f'button{i}']
                                                scrollview.add_subview(button)
                                                button.action = self.open_url
                                            self.has_sent_req = False
                                            self.scrollview = scrollview
                                            self.reader_view = reader_view
                                            scrollview.delegate = self
                                            self.items = deque(self.scrollview.subviews)
                                            assert (len(self.items) - 1) * self.ITEM_H > scrollview.height
                                            self.parent = parent
                                            
                                        def open_url(self, sender):
                                            if sender.url is not None:
                                                self.parent.open_url(sender.url)
                                                console.hud_alert(sender.url)
                                            
                                        def req_data_bg(self):
                                            self.parent.req_data_bg(self)
                                        
                                        def req_data(self, init=False):
                                            self.parent.req_data(self, init)
                                            
                                        def set_navi_view_name(self, name):
                                            self.parent.set_navi_view_name(name)
                                            
                                        def refresh_title(self):
                                            pass
                                                 
                                        def load_data(self):
                                            element = self.parent.load_data(self)
                                            if element is None:
                                                return None
                                            if element[0] is None:
                                                if not element[2]:
                                                    # 到底之后不再复位self.has_sent_req
                                                    console.hud_alert('已经阅读完毕')
                                                return None
                                            chapter, title, init = element
                                            sum_num_lines = 0 if init else 1
                                            if init:
                                                self.set_navi_view_name(title)
                                            for para in chapter:
                                                sum_num_lines += 1
                                                self.contents.append((para))
                                            
                                            split_contents = '—' * self.LEN_LINE
                                            self.contents.append((None, split_contents))
                                            
                                            self.scrollview.content_size += (0, sum_num_lines * self.ITEM_H)
                                            self.has_sent_req = False
                                            return sum_num_lines
                                            
                                        def reset_view(self, i=0, j=0):
                                            scrollview = self.scrollview
                                            # 这里把offset归零了,并调用了函数
                                            scrollview.content_size = (scrollview.width, 0)
                                            
                                            self.cur_offset = 0
                                                
                                            self.contents = []
                                    
                                            self.req_data(True)
                                            sum_num_lines = self.load_data()
                                            if sum_num_lines is None:
                                                for item in self.items:
                                                    item.title = ''
                                                    item.url = None
                                                    
                                                self.items[0].title = '无目录'
                                                self.items[0].y = 0
                                                
                                                return
                                            # 其实应该仿照img模块的,但是没必要,白白把逻辑弄麻烦了
                                            while sum_num_lines <= len(self.items):
                                                self.req_data()
                                                sum_num_lines += self.load_data()
                                            
                                            i = 0
                                            y = 0
                                            for item in self.items:
                                                item.url, item.title = self.contents[i]
                                                # i代表段落index,j代表了段落里面具体的文字起始下标
                                                item.i = i
                                                item.j = j
                                                item.y = y
                                                i += 1
                                                y += self.ITEM_H
                                                
                                        def reset_scrollbar(self):
                                            pass
                                            
                                        def get_UIButton(self,ui_button):
                                            o = ObjCInstance(ui_button)    
                                            for subview in o.subviews():
                                              if subview.isKindOfClass(UIButton):   
                                                 self.UIButton = subview
                                                 break  
                                                      	
                                        def handler(self,_cmd,obj1_ptr):
                                            self.UIButton.title = self.UIButton_title
                                        			
                                        def scrollview_did_scroll(self, scrollview):
                                            offset = scrollview.content_offset.y
                                            is_scroll_down = True if offset > self.cur_offset else False
                                            self.cur_offset = offset
                                            # print('t')
                                            
                                            reader_h = scrollview.height
                                            # print('t')
                                            
                                            content_size = scrollview.content_size[1]
                                            # 预加载
                                            if content_size and content_size - self.cur_offset <= 3.5 * reader_h:
                                                self.req_data_bg()
                                            
                                            # 滚动条下移
                                            if is_scroll_down:
                                                while True:
                                                    item_end = self.items[-1]
                                                    item_start = self.items[0]
                                                    if item_end.y + self.ITEM_H -reader_h < offset and item_end.i is not None:
                                                        i = item_end.i + 1
                                                        if i >= len(self.contents):
                                                            title = self.LOADING
                                                            self.req_data_bg()
                                                            i = None
                                                            url = None
                                                            
                                                        else:
                                                            url, title = self.contents[i]
                                                            
                                                        #============================== begin
                                                        #item_start.title = title    
                                                        self.get_UIButton(item_start)
                                                        self.UIButton_title = title
                                                        block = ObjCBlock(self.handler, restype=None, argtypes=[c_void_p, c_void_p])
                                                        UIButton.animateWithDuration_animations_(0,block)
                                                        #============================== end
                                                        item_start.y = item_end.y + self.ITEM_H
                                                        item_start.i = i
                                                        item_start.url = url
                                                        self.items.rotate(-1)
                                                    elif item_end.i is None and item_end.y <= offset + reader_h:
                                                        if self.load_data() is None:
                                                            break
                                                                        
                                                        item = self.items[-2]
                                                        i = item.i + 1
                                                        j = 0
                                                
                                                        # 由于一定会补足间隔行,所以这里i一定不越界
                                                        text = self.contents[i][0][j: j + self.LEN_LINE]
                                                        
                                                        item_end.text = text
                                                        item_end.y = item.y + self.ITEM_H
                                                        item_end.i = i
                                                        item_end.j = j
                                                        
                                                    else:
                                                        break
                                            else:
                                                while True:
                                                    item_end = self.items[-1]
                                                    item_start = self.items[0]
                                                    
                                                    if item_start.y >= offset:
                                                        i = item_start.i
                                                        if i is None or i <= 0:
                                                            break
                                                        
                                                        i = i - 1
                                                        url, text = self.contents[i]
                                                        #============================== begin
                                                        #item_end.title = text
                                                        self.get_UIButton(item_end)
                                                        self.UIButton_title = text
                                                        block = ObjCBlock(self.handler, restype=None, argtypes=[c_void_p, c_void_p])
                                                        UIButton.animateWithDuration_animations_(0,block)
                                                        #============================== end
                                                        
                                                        item_end.y = item_start.y - self.ITEM_H
                                                        item_end.i = i
                                                        item_end.url = url
                                                        self.items.rotate(1)
                                                        
                                                    else:
                                                        break
                                            self.refresh_title()
                                    
                                    '''
                                    
                                    controller = Controller(EBookLoader, IndexViewer)
                                    
                                    controller.load_reader(url)
                                    controller.navi_viewer.view.present('fullscreen')
                                    '''
                                    
                                    1 Reply Last reply Reply Quote 0
                                    • mikael
                                      mikael @Guest last edited by

                                      @lpl, yes, that is what I meant.

                                      1 Reply Last reply Reply Quote 0
                                      • ?
                                        A Former User last edited by

                                        I hate obj-c. If I have to do every thing with obj-c. Please change the name pythonista into objcista. @omz Would you please check the button.title ?

                                        mikael 1 Reply Last reply Reply Quote 0
                                        • mikael
                                          mikael @Guest last edited by

                                          @lpl, but if you just use a custom view with a label, you do not need any ObjC.

                                          ? 1 Reply Last reply Reply Quote 0
                                          • ?
                                            A Former User @mikael last edited by

                                            @mikael Yes, I'd like to try this.

                                            mikael 1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post
                                            Powered by NodeBB Forums | Contributors