规范 - 使用 ctrl-S (或 cmd+s)保存文本输入

发布于 2025-01-12 15:43:26 字数 276 浏览 2 评论 0原文

SpTextPresenterSpTextInputFieldPresenter 有没有办法使用 ctrl+S (或 mac 中的 cmd+S)来保存文本输入?

旧的 pharo 组件(特别是旧规范,但这是从以前开始的,当组件构建在纯形态上时)允许通过按 来“接受”内容(并通过使用取消版本) )。
有没有办法在当前规范中复制这种行为?

is there a way in SpTextPresenter and SpTextInputFieldPresenter to use ctrl+S (or cmd+S in mac) to save text entry?

Old pharo components (notably old spec but this comes since before, when components when built on plain morphic) were allowing to "accept" contents by pressing <meta+S> (and cancelling the edition by using <meta+L>).
Is there a way to replicate this behavior in current Spec?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自找没趣 2025-01-19 15:43:26

规范允许定义“默认”提交/重置事件以提供旧行为。
好的,这是一个简单的答案,但对于某些用户来说有点复杂,因为他们期望旧的行为,而这不再像以前那样工作了。
所以首先我需要解释为什么旧的行为不再可用:)
事实是,旧组件混合了不同的东西:它们是普通的 UI 小部件,同时也是具有旧 MVC(模型视图控制器)精神的模型容器。所以他们混合了模型(状态的保存)和视图(组件的显示)。因此,旧组件具有初始状态,您需要接受该状态(使用)才能将其转移到模型部分。
这种职责组合导致了不同的解决方法,例如添加 autoAccept 属性以使组件复制其更改的值。
在设计新版本的 Spec 时,我们决定不保留这种看起来很糟糕并且会导致 API 不一致的行为,因此任何想要旧行为的人都需要在自己的组件中显式地实现它。

那么,如何恢复旧行为呢?
这毕竟是个问题!

我们添加了两个方法来实现相同的功能:whenSubmitDo:whenResetDo:。这可以与 whenTextChangedDo: 结合使用来标记/取消标记脏属性。
这是一个示例,有点冗长,但也很容易创建您自己的具有预定义行为的组件并在您的应用程序中重用它们:

app := SpApplication new.

"If using Morphic"
app addStyleSheetFromString: '.application [
    .dirty [ Container { #borderColor: #red, #borderWidth: 1 } ],
    .notDirty [ Container { #borderColor: #transparent, #borderWidth: 1 } ]
]'.

"If using GTK (you need to choose one, both options are not possible at the same time)"
app useBackend: #Gtk.
app addCSSProviderFromString: '
.dirty { 
    border-color: red; 
    border-width: 1px; }
'.
 
presenter := SpPresenter new.
presenter application: app.

presenter layout: (SpBoxLayout newTopToBottom
    add: (textPresenter := presenter newTextInput) expand: false;
    yourself).

text := ''.
textPresenter 
    text: text;
    whenTextChangedDo: [ :aString | 
        aString = text
            ifTrue: [ textPresenter removeStyle: 'dirty'; addStyle: 'notDirty' ]
            ifFalse: [ textPresenter removeStyle: 'notDirty'; addStyle: 'dirty' ] ];
    whenSubmitDo: [ 
        text := textPresenter text.
        ('Submitted ', text) crTrace.
        textPresenter 
            removeStyle: 'dirty'; 
            addStyle: 'notDirty' ];
    whenResetDo: [ 
        textPresenter 
            text: text;
            removeStyle: 'dirty';
            addStyle: 'notDirty' ]. 

presenter asWindow 
    title: 'Example submit/reset text component';
    open

这将产生(使用 Gtk3 后端)此输出:

Spec permits to define "default" submit/reset events to provide old behavior.
Ok, this is an easy answer, but somehow complicated for some users since they expect the old behavior and this does not works like that anymore.
So first I need to explain why old behavior is no longer available :)
Thing is, old components where a mix of different things: they were plain UI widgets while also model containers in the spirit of old MVC (Model View Controller). So they mixed Model (the keep of the status) and the view (the display of the component). For this reason, old components had an initial status and you needed to accept that status (you got it, using <meta+S>) to make it being transferred to the model part.
This mix of responsibilities lead to different workarounds, like the addition of the autoAccept property to make the component copy its value it change of it.
When designing the new version of Spec we decided to not keep this behavior that looked hacky and was causing inconsistencies in the API and in consequence anyone wanting the old behavior need to make it explicitly in their own components.

So, how to get old behavior?
This is the question after all!

We have added two methods to allow somehow same functionality: whenSubmitDo: and whenResetDo:. This can be combined with whenTextChangedDo: to mark/unmark a dirty property.
Here is an example, Is a little bit verbose, but is also easy to create your own components with this behavior predefined and reuse them in your application:

app := SpApplication new.

"If using Morphic"
app addStyleSheetFromString: '.application [
    .dirty [ Container { #borderColor: #red, #borderWidth: 1 } ],
    .notDirty [ Container { #borderColor: #transparent, #borderWidth: 1 } ]
]'.

"If using GTK (you need to choose one, both options are not possible at the same time)"
app useBackend: #Gtk.
app addCSSProviderFromString: '
.dirty { 
    border-color: red; 
    border-width: 1px; }
'.
 
presenter := SpPresenter new.
presenter application: app.

presenter layout: (SpBoxLayout newTopToBottom
    add: (textPresenter := presenter newTextInput) expand: false;
    yourself).

text := ''.
textPresenter 
    text: text;
    whenTextChangedDo: [ :aString | 
        aString = text
            ifTrue: [ textPresenter removeStyle: 'dirty'; addStyle: 'notDirty' ]
            ifFalse: [ textPresenter removeStyle: 'notDirty'; addStyle: 'dirty' ] ];
    whenSubmitDo: [ 
        text := textPresenter text.
        ('Submitted ', text) crTrace.
        textPresenter 
            removeStyle: 'dirty'; 
            addStyle: 'notDirty' ];
    whenResetDo: [ 
        textPresenter 
            text: text;
            removeStyle: 'dirty';
            addStyle: 'notDirty' ]. 

presenter asWindow 
    title: 'Example submit/reset text component';
    open

This will produce (with the Gtk3 backend) this output:

editing text and reacting to it

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文