GWT CSS资源定制
我正在使用 UIBinder 和 MVP 编写 GWT 小部件。小部件的默认样式在 TheWidgetView.ui.xml 中定义:
<ui:style type="com.widgetlib.spinner.display.TheWidgetView.MyStyle">
.textbox {
border: 1px solid #red;
}
.important {
font-weight: bold;
}
</ui:style>
小部件的 CssResource 接口在 TheWidgetView.java 中定义:
public class TheWidgetView extends HorizontalPanel implements TheWidgetPresenter.Display {
// ... some code
@UiField MyStyle style;
public interface MyStyle extends CssResource {
String textbox();
String important();
}
// ... more code
}
我希望此小部件的使用者能够自定义小部件的部分样式并将其包含在他们的 MyExample.ui.xml 中:
<ui:style type="com.consumer.MyExample.MyStyle">
.textbox {
border: 2px solid #black;
}
</ui:style>
这就是他们的 MyExample.java:
public class MyExample extends Composite {
// ... some code
@UiField MyStyle style;
interface MyStyle extends TheWidgetView.MyStyle{
String textbox();
}
// ... more code
}
有没有办法让我的小部件可以拥有默认样式,但小部件的使用者可以覆盖其中之一?当接口扩展 TheWidgetView.MyStyle 时,小部件使用者需要定义该父接口中列出的所有样式。我见过一些小部件库的小部件构造函数采用 ClientBundle 作为参数,我认为这可以应用于 CssResource。虽然,我不确定如何在 UIBinder 调用的构造函数中传递此样式对象。
提前非常感谢!
I'm writing a GWT widget using UIBinder and MVP. The widget's default styles are defined in TheWidgetView.ui.xml:
<ui:style type="com.widgetlib.spinner.display.TheWidgetView.MyStyle">
.textbox {
border: 1px solid #red;
}
.important {
font-weight: bold;
}
</ui:style>
The widget's CssResource interface is defined in TheWidgetView.java:
public class TheWidgetView extends HorizontalPanel implements TheWidgetPresenter.Display {
// ... some code
@UiField MyStyle style;
public interface MyStyle extends CssResource {
String textbox();
String important();
}
// ... more code
}
I'd like the consumer of this widget to be able to customize part of the widget's styles and to have this in their MyExample.ui.xml:
<ui:style type="com.consumer.MyExample.MyStyle">
.textbox {
border: 2px solid #black;
}
</ui:style>
And this be their MyExample.java:
public class MyExample extends Composite {
// ... some code
@UiField MyStyle style;
interface MyStyle extends TheWidgetView.MyStyle{
String textbox();
}
// ... more code
}
Is there a way that my widget can have default styles, but that the consumer of the widget can override one of them? When an interface extends TheWidgetView.MyStyle, the of the widget consumer needs to define all the styles listed in that parent interface. I've seen some widget libraries have the widget's constructor take in a ClientBundle as parameter, which I suppose could apply to CssResource. Although, I'm not sure how I'd pass in this style object in a constructor invoked by UIBinder.
Thanks much in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我尝试过类似的方法来使我的应用程序可换肤。我将首先查看任何单元格小部件的源代码。他们似乎将资源作为构造函数,但是,如果未提供资源,他们也有使用 GWT.create(Resources.class) 来创建资源的构造函数。至于您关于将此模板用于 UIBinder 的问题,提到了 3 个选项 在这里。然而,当尝试在 UIBinder 中定义样式时,您可能会遇到先有鸡还是先有蛋的问题。
您在其他代码中遇到的问题是您的样式有两种不同的实现,因为 uibinder 创建了自己的资源包,该资源包不引用父级的 css。有 3 种解决方案:
1) 将 css 从 ui 绑定器文件中撕下来到它自己的文件中,并使用 ui:with 与提供的字段或 uifactory 结合使用,以使用您自己的资源绑定注入样式,您可以在其中复合源(即@Source({DEFAULT_CSS, "myCss.css"}))。
2)您的另一个选择是查看生成的代码并使用它们用来引用 uibinder 文件中的 css 的语法,但是您必须克服两个问题:先有鸡还是先有蛋的问题以及 google 的事实可以在不告诉你的情况下改变这一点并破坏你的东西。以下是从我的一个文件生成的客户端包的示例:
3) 最后一种解决方案是使用延迟绑定来替换默认样式。
I have playing around with something similar to make my application skinable. I would start by looking at the source code for any of the cell widgets. They seem to take the resources as a constructor, however they also have constructors that use GWT.create(Resources.class) to create the resources if they are not provided. As far as your question about using this template for UIBinder, there are 3 options mentioned here. However you may run into chicken and the egg issues when trying to define the style inside the UIBinder.
The issue you are running into with your other code is that your 2 different implementations of the style because uibinder creates it's own resource bundle which doesn't reference the parent's css. There are 3 solutions:
1) Tear the css out of the ui binder file into it's own file and use ui:with combined with either a provided field or uifactory to inject the style using your own resource bindle where you can compound the sources (i.e. @Source({DEFAULT_CSS, "myCss.css"})).
2) Your other option is to look at the generated code and use the syntax they are using to refernce the css within the uibinder file, however there are 2 issues you will have to overcome: the chicken and the egg problem and the fact that google can change this without telling you and break your stuff. Here is an example of the generated client bundle from one of my files:
3) The last solution is to use deferred binding to replace the default style.