在 GWT 中需要应用程序范围的 CSS 常量

发布于 2024-09-15 11:48:18 字数 983 浏览 2 评论 0原文

我想在 GWT CssResource 中将一些颜色定义为常量,并在整个应用程序中使用这些常量;但我不知道该怎么做。

我会告诉你我已经尝试过什么。我创建了一个 ClientBundle 和一个 CssResource,如下所示:

public interface Resources extends ClientBundle {
  public interface MyStyle extends CssResource {
    String JUNGLEGREEN();
    String example();
    ...
  }
  @Source("Resources.css")
  MyStyle css();
}

我定义了一些 Resources.css 中的常量

@def JUNGLEGREEN #1F3D0A;

在 Resources.css 中,我像这样使用这些常量:

.example { color:JUNGLEGREEN; }

我不知道如何在其他 CSS 文件和 UiBinder 模板中重用这些常量。我想在其他一些 UiBinder 文件中执行此操作,例如 LoginView.ui.xml:

<ui:with field='resources' type='com.example.Resources' />
<ui:style>
  .mainPanel {
    background:{resources.css.JUNGLEGREEN};
    ...
  }
</ui:style>

...但它似乎无法编译。你知道我怎样才能实现我的目标吗?

I'd like to define some colours as constants in a GWT CssResource, and use those constants throughout my application; but I don't know how to do that.

I'll tell you what what I've tried. I've created a ClientBundle and a CssResource as follows:

public interface Resources extends ClientBundle {
  public interface MyStyle extends CssResource {
    String JUNGLEGREEN();
    String example();
    ...
  }
  @Source("Resources.css")
  MyStyle css();
}

I've defined some constants in Resources.css:

@def JUNGLEGREEN #1F3D0A;

Within Resources.css, I use those constants like so:

.example { color:JUNGLEGREEN; }

I'm not aware of a way to re-use those constants in other CSS files and UiBinder templates. I'd like to do this in some other UiBinder file, say LoginView.ui.xml:

<ui:with field='resources' type='com.example.Resources' />
<ui:style>
  .mainPanel {
    background:{resources.css.JUNGLEGREEN};
    ...
  }
</ui:style>

...but it doesn't seem to compile. Do you know how I can achieve my objective?

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

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

发布评论

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

评论(3

烟花易冷人易散 2024-09-22 11:48:18

我们是这样做的:

  • 我们将所有常量属性放在
@def black #241b15;   /* text color */
@def orange #ff4f00;   /* links */
  • 每个 ui.xml 文件中的 constant.css 文件中,您可以通过以下方式引用这些常量:
<ui:style src="../../resources/css/constants.css">
    .myStyle {
        color: orange;
    }
</ui:style>

希望有所帮助。

编辑:

为了避免 元素中的相对路径,您可以执行以下操作:

  • 在 css 文件中再次定义常量(例如 constants.css
@def junglegreen #1f3d0a;
  • 创建一个 ClientBundle 和 CssResource 来检索定义的常量
public interface MyResources extends ClientBundle {

    public static final MyResources INSTANCE = GWT.create(MyResources.class);

    public interface Constants extends CssResource {

        String junglegreen();
    }

    Constants constants();
}

- 使用 @eval 注释来访问常量

<ui:style>
    @eval green com.gwt.client.widget.test.MyResources.INSTANCE.constants().junglegreen();

    .someClass {
        color: green;
    }
</ui:style>

我知道如何处理的唯一方法使用常量而不引用 css 文件本身。

This is how we do it:

  • we place all our constant attributes in a constant.css file
@def black #241b15;   /* text color */
@def orange #ff4f00;   /* links */
  • in each ui.xml file you can reference to those constants the following way:
<ui:style src="../../resources/css/constants.css">
    .myStyle {
        color: orange;
    }
</ui:style>

Hope that helps.

EDIT:

To avoid the relative path in the <ui:style> element you could do the following:

  • define your constants again in a css file (say constants.css)
@def junglegreen #1f3d0a;
  • create a ClientBundle and CssResource to retrieve the defined constants
public interface MyResources extends ClientBundle {

    public static final MyResources INSTANCE = GWT.create(MyResources.class);

    public interface Constants extends CssResource {

        String junglegreen();
    }

    Constants constants();
}

-use the @eval annotation to access the constant

<ui:style>
    @eval green com.gwt.client.widget.test.MyResources.INSTANCE.constants().junglegreen();

    .someClass {
        color: green;
    }
</ui:style>

The only way I know of how to deal with constants without referencing the css file itself.

笑咖 2024-09-22 11:48:18

我知道这个答案可能有点晚了,但可能会对某人有所帮助。我遇到了同样的问题,并且能够通过添加以下内容来解决它:

Resources.css().ensureInjected()

我在我的工厂中添加了它,但在几个地方尝试了它,无论我把它放在哪里,它都有效。

I know this answer might be kind of late but may help someone. I was having the same problem and was able to solve it by adding the following:

Resources.css().ensureInjected()

I added it in my factory but tried it in a couple of places and no matter where I put it, it worked.

淡淡绿茶香 2024-09-22 11:48:18

你应该能够使用

<ui:style>
  @IMPORT url("../../../global.css");
  .mainPanel {
    background:{resources.css.JUNGLEGREEN};
    ...
  }
</ui:style>

You should be able to use

<ui:style>
  @IMPORT url("../../../global.css");
  .mainPanel {
    background:{resources.css.JUNGLEGREEN};
    ...
  }
</ui:style>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文