Skip to content

Conversation

@amorphobia
Copy link
Contributor

@amorphobia amorphobia commented Mar 3, 2021

This change export Rime::Switcher to Lua, so that users can directly manipulate it in a lua_processor.

My situation is to toggle a switch through selecting candidates, instead of key bindings. Following is partial of the processor's code:

local function processor(key, env)
  local context = env.engine.context
  local switcher = Switcher(env.engine)
  local user_config = switcher.user_config

  -- get option_name and some_bool_value

  if --[[ some condition --]] then
    context:set_option(option_name, some_bool_value)
    if switcher:is_auto_save(option_name) then
      user_config:set_bool("var/option/" .. option_name, some_bool_value)
    end
    context:clear()
    return kAccepted
  end
end

@hchunhui
Copy link
Owner

hchunhui commented Mar 3, 2021

Looks good, but do we really need Switcher?
I think a commit notifier should be enough: #23 (comment)

@amorphobia
Copy link
Contributor Author

Looks good, but do we really need Switcher?
I think a commit notifier should be enough: #23 (comment)

I'll try it, but I think Switcher provide more convenience in some situations. I have following concerns:

  1. Is a notifier capable to call the callback function without actually commit the text? I'm not familiar with notifiers. Would you mind to show me an example? Thanks.
  2. Switcher provides some handy functions like IsAutoSave. Though we can implement it in Lua by traversing the config under "switcher/save_options", IsAutoSave might be easier.
  3. Is there a way to access Switcher::user_config without exporting Switcher? It's first loaded here. To get the user config without Switcher, we still need to add some functions.

@amorphobia
Copy link
Contributor Author

traversing the config

Actually we cannot traverse the config untill Config::GetList has been wrapped.

@hchunhui
Copy link
Owner

hchunhui commented Mar 4, 2021

OK, Switcher provides more convenience. Exporting more low level functions is a long-term plan.

Please also commit a simple script that uses Switcher as an example. After that I will merge the PR.

@shewer
Copy link
Contributor

shewer commented Mar 5, 2021

Looks good, but do we really need Switcher?
I think a commit notifier should be enough: #23 (comment)

I'll try it, but I think Switcher provide more convenience in some situations. I have following concerns:

  1. Is a notifier capable to call the callback function without actually commit the text? I'm not familiar with notifiers. Would you mind to show me an example? Thanks.

{ "commit_notifier", WRAPMEM(T::commit_notifier) },

https://github.com/rime/librime/blob/99e269c8eb251deddbad9b0d2c4d965b228f8006/src/rime/context.h#L21

function init(env)
       local  commit_notifier=env.engine.context.commit_notifier
       env. commit_notifier_connected = commit_notifer:connect( function(ctx)  ... end )
       env.option_update_notifier_connected=env.engine.context.option_update_notifier:connect( function(ctx,option_name) 
       if option_name== "ascii_mode" then  
       end
end 

)  
end 
function fini(env)
      env.commit_notifier_connected:disconnect()
      env.option_update_notifier_connected:disconnect()
end 


  1. Switcher provides some handy functions like IsAutoSave. Though we can implement it in Lua by traversing the config under "switcher/save_options", IsAutoSave might be easier.
  2. Is there a way to access Switcher::user_config without exporting Switcher? It's first loaded here. To get the user config without Switcher,

@hchunhui hchunhui merged commit 88ba821 into hchunhui:master Mar 5, 2021
amorphobia added a commit to amorphobia/openfly that referenced this pull request Mar 8, 2021
With the [PR](hchunhui/librime-lua#92) merged, now lua can toggle
a switch through selecting a candidate without actually commit the
text.

Close #36
amorphobia added a commit to amorphobia/openfly that referenced this pull request Mar 8, 2021
With the [PR](hchunhui/librime-lua#92) merged, now lua can toggle
a switch through selecting a candidate without actually commit the
text.

Close #36
@amorphobia amorphobia deleted the switcher branch March 8, 2021 02:30
@shewer
Copy link
Contributor

shewer commented Apr 3, 2021

請教一下 SwitcherReg make() 爲何 用 New Switcher(engine)
swicher.cc 沒有 Switcher::Switcher(engine)
只有 Switcher(const Ticket* ticket)

另外 switcher:active() ,menu 無法操作

@amorphobia
Copy link
Contributor Author

amorphobia commented Apr 6, 2021

我看到 engine.cc 里直接使用了 New<Switcher>(this), 所以我直接拿来用了。

以下是我的猜测

第一是因为 SwitcherEngine 的 public 继承

第二是 New 这个 function 本身只生成了一个新的 shared_ptr<T>, 其参数是 rvalue, 会不会根本就没有调用 T 的 constructor?

至于 active, 我还不了解其作用,所以没有在 lua 里使用过,可能需要一个新 issue 来讨论吧。

shewer added a commit to shewer/librime-lua that referenced this pull request Apr 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants