on_current_view_changed

edited January 26 in General

Hi all,
In the manual is written:
on_current_view_changed: This event is triggered when the current tab changes. The signal is available with an integer parameter: this is the index of the previous tab. The new tab is already the current tab when this event is triggered.

$mw = Application.instance.main_window
$code = proc { |last| puts "viewchanged #{last==nil}" }
$mw.on_current_view_changed=$code

worked but last is always nil.
When I substitute proc with lambda it is not working.

However you can work with a global variable saving $mw.current_view

In the end

$mw = Application.instance.main_window
$lastview=$mw.current_view
$mw.on_current_view_changed-=$code1_current_view_changed
$code1_current_view_changed = proc { puts "viewchanged #{$lastview}"; $lastview=$mw.current_view}
$mw.on_current_view_changed+=$code1_current_view_changed

worked fine for me.
(The line 3 can be obmitted, if the code is called only once.)
Best regards,
Frank

Comments

  • edited January 27

    Hi Frank

    you can also simple use a block instead of a proc:

    mw = RBA::Application.instance.main_window
    mw.on_current_view_changed do |last|
      puts "viewchanged #{last.inspect}"
    end
    

    But you're right about the nil. The documentation was incorrect - there is no such parameter :( The master is already updated.

    But you do not need a global variable. This works for MainWindow as there is one one. But that's not a solution in other cases. One solution is to extend MainWindow to provide a new attribute you can use:

    module RBA
      class MainWindow
        attr_accessor :last_view
      end
    end
    
    mw = RBA::Application.instance.main_window
    mw.on_current_view_changed do
      puts "viewchanged #{mw.last_view ? mw.last_view.title : 'nil'} -> #{mw.current_view ? mw.current_view.title : ''}"
      mw.last_view = mw.current_view
    end
    

    Another approach is a more object-oriented one which works with an observer object:

    class CurrentViewObserver
      def initialize
        @mw = RBA::Application.instance.main_window
        @mw.on_current_view_changed += lambda { self.view_changed }
        @last_view = nil
      end
      def view_changed
        puts "viewchanged #{@last_view ? @last_view.title : 'nil'} -> #{@mw.current_view ? @mw.current_view.title : ''}"
        @last_view = @mw.current_view
      end
    end
    
    CurrentViewObserver::new
    

    Kind regards,

    Matthias

Sign In or Register to comment.