Icon not visible in Lumino widget title - jupyter-lab

I am trying to add a Lumino widget to the 'right' panel of the Jupyter Lab UI. However, I am not able to set the icon in the title bar of the widget using font awesome icons. Here's my code:
class ExampleWidget extends Widget {
constructor() {
super()
this.id = 'myextension-widget'
this.title.label = 'Sample'
this.title.closable = true
this.title.iconClass = 'face-smile'
this.addClass('mypanel')
}
}
What am I missing here? Can someone please help?

You need to:
had the 'fa' class
fix the font class name ('fa-smile-o' instead of 'face-smile')
This example works for me:
class ExampleWidget extends Widget {
constructor() {
super()
this.id = 'myextension-widget'
this.title.label = 'Sample'
this.title.closable = true
this.title.iconClass = 'fa fa-smile'
this.addClass('mypanel')
}
}

Related

#Published Attributed Strings Not Working

Attributed Strings don't seem to retain their attributes when created in the ViewModel and passed to a SwiftUI View. Is there a way to make #Published strings with bold/italics/etc in a ViewModel that retain their attributes in the View?
The following correctly applies bold styling
struct ContentView: View {
var body: some View {
Text( "**This** should be bold")
}
}
However, when using a ViewModel it produces: **This** should be bold
final class ViewModel: ObservableObject {
#Published var attributedString = "**This** should be bold"
}
struct ContentView: View {
#ObservedObject private var viewModel = ViewModel()
var body: some View {
Text(viewModel.attributedString)
}
}
I've also tried setting the type as AttributedString, however that doesn't seem to make a difference.
You can use AttributedString(markdown: ) but as it throws, you have to use it e.g.in the init.
final class ViewModel: ObservableObject {
#Published var attributedString: AttributedString
init() {
self.attributedString = ""
try? attributedString = AttributedString(markdown: "**This** should be bold")
}
}

How to add onClickListener on images in recyclerView?

I am trying to attach onclick listener on images in this code, using Glide framework but I'm not able to find a solution for that. Can someone help with this?
Images are showing in emulator already.
Thanks in advance
package com.example.andoridlifecycle.adapters
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.example.andoridlifecycle.R
import kotlinx.android.synthetic.main.item_custom_row.view.*
class ItemAdapter(private val context: Context, private val urls: ArrayList<String>) :
RecyclerView.Adapter<ItemAdapter.ViewHolder>() {
/**
* Inflates the custom view which is designed in xml layout file
*/
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(
LayoutInflater.from(context).inflate(
R.layout.item_custom_row,
parent,
false
)
)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
// Array with urls
val url = urls[position]
Glide.with(context)
.load(url)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.placeholder(R.drawable.placeholder)
.into(holder.imageView)
}
// Gets the number of items in the list
override fun getItemCount(): Int {
return urls.size
}
// A ViewHolder describes an item view and metadata about its place within the RecyclerView.
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val imageView: ImageView = view.iv_image
}
}
I'm Java Developer so I cannot code with Kotlin but I'll show you how to set OnClickListener on your imageView. For using OnclickListener on imageView no need for Glide Library.
Just add the below lines in your onBindViewHolder
holder.imageView.setOnClickListener(view -> {
Toast.makeText(context, "this is number: "+position+" Image Selected", Toast.LENGTH_SHORT).show();
// or your code for imageView
});
Your new code looks like the below:-
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
// Array with urls
val url = urls[position]
//code for onClick on image
holder.imageView.setOnClickListener(view -> {
Toast.makeText(context, "this is number: "+position+" Image Selected", Toast.LENGTH_SHORT).show();
// or your code for imageView
});
Glide.with(context)
.load(url)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.placeholder(R.drawable.placeholder)
.into(holder.imageView)
}
Make sure to change JAVA to Kotlin in my code. If you any problem is causing, Let me know

How do I assign contents of a string to a text field in Unity 4.6?

I am working on a C# class that imports text data from a web site. That works OK. Where I'm losing it is how to display the text in Unity 4.6 once I have all the text in a string variable. Any advice is appreciated.
Unity 4.6 UI system has component named Text. You can watch video tutorial here. Also I suggest you to check out its API.
As always, you have two options on how to add this component to the game object. You can do it from editor (just click on game object you want to have this component in hierarchy and add Text component). Or you can do it from script using gameObject.AddComponent<Text>().
In case you are not familiar with components yet, I suggest you to read this article.
Anyway, in your script you'll need to add using UnityEngine.UI; at the very top of it, because the Text class is in UnityEngine.UI namespace. Ok, so now back to script that will set the value of Text component.
First you need variable that refers to Text component. It can be done via exposing it to editor:
public class MyClass : MonoBehaviour {
public Text myText;
public void SetText(string text) {
myText.text = text;
}
}
And attaching gameObject with text component to this value in Editor.
Another option:
public class MyClass : MonoBehaviour {
public void SetText(string text) {
// you can try to get this component
var myText = gameObject.GetComponent<Text>();
// but it can be null, so you might want to add it
if (myText == null) {
myText = gameObject.AddComponent<Text>();
}
myText.text = text;
}
}
Previous script is not a good example, because GetComponent is actually expensive. So you might want to cache it’s reference:
public class MyClass : MonoBehaviour {
Text myText;
public void SetText(string text) {
if (myText == null) {
// looks like we need to get it or add
myText = gameObject.GetComponent<Text>();
// and again it can be null
if (myText == null) {
myText = gameObject.AddComponent<Text>();
}
}
// now we can set the value
myText.text = text;
}
}
BTW, the patter of ‘GetComponent or Add if it doesn’t exist yet’ is so common, that usually in Unity you want to define function
static public class MethodExtensionForMonoBehaviourTransform {
static public T GetOrAddComponent<T> (this Component child) where T: Component {
T result = child.GetComponent<T>();
if (result == null) {
result = child.gameObject.AddComponent<T>();
}
return result;
}
}
So you can use it as:
public class MyClass : MonoBehaviour {
Text myText;
public void SetText(string text) {
if (myText == null) {
// looks like we need to get it or add
myText = gameObject.GetOrAddComponent<Text>();
}
// now we can set the value
myText.text = text;
}
}
make sure you import the ui library - using UnityEngine.UI
gameObject.GetComponent<Text>().text - replace .text with any other field for UI Text
I assume that the issue is creating dynamic sized "textbox" rather than just assigning the string to a GUIText GameObject. (If not - just put a GUIText GameObject into your scene, access it via a GUIText variable in your script and use myGUIText.text = myString in Start or Update.)
If I am correct in my assumption, then I think you should just be using a GUI Label:
http://docs.unity3d.com/ScriptReference/GUI.Label.html
If you need to split the string up to place text into different labels or GUITexts, you will need to use substrings

Can't use embed font

I'm doing everything just like in the instruction.
Class Fonts.hx
import flash.text.Font;
#:font("assets/fonts/OPENSANS-REGULAR_0.TTF") class OpenSansFont extends Font { }
class Fonts
{
public static inline var OPEN_SANS_PATH = "assets/fonts/OPENSANS-REGULAR_0.TTF";
public static inline var OPEN_SANS_FONTNAME = "OPENSANS-REGULAR_0.TTF";
public function new()
{
Font.registerFont(OpenSansFont);
}
}
But when I try create TextFormat with this:
var tf:TextFormat;
var openSans:Font = Assets.getFont(Fonts.OPEN_SANS_PATH);
tf = new TextFormat(openSans.fontName);
I catch this error:
Assets.hx:257: [openfl.Assets] There is no Font asset with an ID of
"assets/fonts/OPENSANS-REGULAR_0.TTF"
What am I doing wrong?
My project structure:
You can't use openfl.Assets for Assets embedded via #:font / #:bitmap etc.
You should use the font's name for the TextFormat constructor. I assume you've already tried that, seeing how there's an OPEN_SANS_FONTNAME variable. However, that's not the font's name, just its file name.
On Windows, you should be able to find out the name by double-clicking on the font (right under the print / install buttons).
Alternatively, this should work as well:
import flash.text.Font;
#:font("assets/fonts/OPENSANS-REGULAR_0.TTF") class OpenSansFont extends Font { }
class Fonts
{
public static var OPEN_SANS_FONTNAME;
public function new()
{
Font.registerFont(OpenSansFont);
OPEN_SANS_FONTNAME = new OpenSansFont().fontName;
}
}

JavaFX VBox and HBox layouts

I'm working on a JavaFX application which has a layout generated from an external data structure, composed of
displaying components that know their own aspect ratios (height as a dependent of width)
2 types of structural components that
display all children with an equal width across their page, each child up as much vertical space as needed
display all children down the page using the full width and taking as much vertical space as needed
But I'm finding things aren't displaying as I expect. I've made a simplified case that demonstrates the problem.
The code is below, and the problem is that v3 doesn't get displayed, and I can't for the life of me work out why. I guess there's some facet of VBoxes and HBoxes that I haven't understood.
I'd really appreciate any help or ideas. Thanks in advance!
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import java.util.Random;
public class Test extends Application {
static Random rand = new Random();
public static void main(String args[]) {
Application.launch("something");
}
#Override
public void start(Stage mainStage) throws Exception {
testVBoxes(mainStage);
}
private void testVBoxes(Stage mainStage) {
VBox root = new VBox();
Scene one = new Scene(root, 800, 600, Color.WHITE);
FixedAspectRatioH h1 = new FixedAspectRatioH();
FixedAspectRatioH h2 = new FixedAspectRatioH();
FixedAspectRatioH h3 = new FixedAspectRatioH();
FixedAspectRatioV v1 = new FixedAspectRatioV();
FixedAspectRatioV v2 = new FixedAspectRatioV();
FixedAspectRatioV v3 = new FixedAspectRatioV();
h1.prefWidthProperty().bind(root.widthProperty());
h2.add(v2);
v1.add(h3);
v1.add(h2);
h1.add(v1);
h1.add(v3);
root.getChildren().add(h1);
mainStage.setScene(one);
mainStage.show();
}
private class FixedAspectRatioV extends VBox {
public FixedAspectRatioV() {
Rectangle r = new Rectangle();
r.setFill(Color.rgb(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)));
r.widthProperty().bind(widthProperty());
r.heightProperty().bind(r.widthProperty().divide(3));
getChildren().add(r);
}
public void add(Region n) {
n.prefWidthProperty().bind(widthProperty());
getChildren().add(n);
}
}
private class FixedAspectRatioH extends HBox {
public FixedAspectRatioH() {
Rectangle r = new Rectangle();
r.setFill(Color.rgb(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)));
r.widthProperty().bind(widthProperty().divide(4));
r.heightProperty().bind(r.widthProperty());
getChildren().add(r);
}
public void add(Region n) {
HBox.setHgrow(n, Priority.ALWAYS);
getChildren().add(n);
}
}
}
its 2 years but the solution is you forgot
Node.setPrefSize(width,height);
and also add this to your constructor Hbox.setFillHeight(true);

Resources