codefoster | to inform and to inspire
Jeremy Foster
@codefoster

Horizontal Panning

by Jeremy Foster 20. August 2012 12:56

If you drop a ListView into your HTML page and fill it with data that fills up your page and overflows, what happens to the overflow? The answer is that it gets cut off by the right side of the screen and thus hints to the user to swipe to scroll the rest of the content into view. Easy.

But what if you aren’t using a ListView, or what if you have some content that you want to show next to your ListView and you want them to both pan together when the user swipes?

Well, I’m going to tell you.

The answer in short is… overflow-x: scroll.

And the answer in long follows.

Try this simple HTML.

<section aria-label="Main content" role="main">
    <div class="sidescroll">
        <p>Now is the time ... his country.</p> 
        ...
        <p>Now is the time ... his country.</p> 
    </div>
</section>

The section should already be defined in you app if you started with a project template, so you should only need to define the div. Where I’ve put ellipses (…) you should add a bunch of text so that this div contains more text than a single screen should hold.

Now add some CSS to make this act the way you want. This should do it…

.mypage section[role=main] .sidescroll {
    height:600px;
    overflow-x: scroll;
    column-width: 300px;
}

(Note that this style rule starts with .mypage. If you are using the navigation template, then that is necessary to scope the style rule to this page only and keep it from affecting other pages. If you didn’t start with the navigation template or if the style rule isn’t working for you, then try simply removing that.)

The magic (well, it’s just science actually) line there is overflow-x: scroll. If we just used overflow: scroll then the div would try to allow scrolling on both axes. We only want horizontal scrolling though, so overflow-x is the property of choice.

Sometimes you want one element to stay fixed while the rest of the page pans. In that case, you would just drop the element outside of and before this scrolling div. Easy peasy.

That’s it. Happy panning!

Tags: , , , , , , , , ,

CSS | HTML | JavaScript | Windows 8

Windows 8 App Bar Icons (from Segoe UI)

by Jeremy Foster 18. August 2012 23:04

UPDATE: since writing this I've found this MSDN article that I think has more information than my post. Hope that's helpful.

This is a reference post. Come back when you, like me, forget which icons are available to use on your application bar in your Windows 8 app. A larger version is attached here: AppBarIcons.jpg (223.22 kb).

If you define your app bar imperatively (in JavaScript), then you would simply pass the name of the icon from this graphic in as the icon option like so…

new WinJS.UI.AppBarCommand({..., icon:'bookmarks', ...})

Here’s the chart for those with young eyes :)

AppBarIcons

Tags: , , , , , , , , , , , , , , , , , , , , , , ,

Windows 8

Getting Windows Device Info

by Jeremy Foster 17. August 2012 16:18

I have plenty of experience in the C# space with accessing Windows API, but I’m still finding it rather novel and delightful to do the same thing in JavaScript. The fact that I can do something like…

Windows.Devices.Enumeration.DeviceInformation

…is just slick. That’s all. There’s no interop’ing, no dll loading, no service calls. WinRT just delivers it to my front door and doesn’t even make me sign.

Recently, I went looking for how to enumerate the devices currently recognized by the system and found it to be quite nice and thought I’d share.

I started by creating a ListView and an item template in the HTML and imperatively binding that to a WinJS.Binding.List in my JavaScript file. In the interest of being DRY, I won’t walk through that process, but you can see the concept here.

With a ListView and a WinJS.Binding.List in place and with the two hooked together, we’re ready to just fetch our device information and push it into the List. I’ll just lay out all the JS code at once and then explain. Perhaps it will be self-explanatory.

Windows.Devices.Enumeration.DeviceInformation.findAllAsync().done(function (devices) {
    devices
        .filter(function (d) { return d.name.length > 0 && d.isEnabled; })
        .distinct(function (d) { return d.name; })
        .forEach(function (d) {
            d.getGlyphThumbnailAsync().then(function(thumbnail) {
                if (thumbnail && thumbnail.size > 0) {
                    devicesList.push({
                        imageUrl: URL.createObjectURL(thumbnail, { oneTimeOnly: false }),
                        name: d.name
                    });
                }
            });
        });
});

If you follow me much, you likely know that my code tends to be rather dense. I like using the horizontal space that God (and Visual Studio) gave me instead of wearing out my enter key and your scroll wheel. So there are a few things going on in this relatively short snippet.

First, I’m calling into Windows.Devices.Enumeration.DeviceInformation and calling findAllAsync(). That will asynchronously return all of the devices found on the system.

.done() is how we proceed with the results of an asynchronous call in case you haven’t seen that before, and we have a chance to capture the async payload (in this case a bunch of devices) by specifying a devices paramter.

Next, I’m calling a few array functions.

The filter function takes a lambda function and in this case I’m only concerned with devices that have a name and are enabled.

The distinct function is my own. If you want the code for that one, leave me a comment. It reduces the array to only those with unique entries, and it gives you an opportunity to specify what you mean by “unique”. In this case, I’m saying that two devices are distinct if their name values are distinct.

Then I do a fancy forEach on the array. Notice how all of these array functions themselves return arrays making it convenient to chain functions together. The forEach function simply calls the provided function on each of the items in the list. No more (or far fewer at least) awkward for iterations. Yay!

In this forEach function, I’m doing another async call - this time to retrieve the Windows 8-style, super fancy glyph graphic to represent the device (hint: you can also call getThumbnailAsync() to get a boring, old-style, full color, supposedly realistic icon for each device.

When the call returns, I create an anonymous object shaped like the data that my template is expecting and push it into my WinJS.Binding.List. Just like that.

A little bit of CSS work later we have something that looks like this…

image

If you have any questions, just leave a comment and I’ll approve and respond as soon as I can.

Have device enumerating!

Tags: , , ,

JavaScript | Windows 8

Learn Windows 8 Development in Your Underwear

by Jeremy Foster 13. August 2012 19:48

If you’re interested in learning how to write apps for Windows 8 but don’t want to leave your desk to do it, then I have just the thing for you. They’re called HOLOs. That’s a Hands-On Lab Online. It goes like this.

First, you register and then when the event comes around, you log in and are hooked up with a Live Meeting to a presenter and a bunch of other attendees and also with a Hyper-V session to a Windows 8 virtual machine with all of the course work ready to go. You listen to the presenter for a while, and then you run through the labs with the instructor right there and ready to help when you raise your virtual hand.

There are sessions on a myriad of Windows 8 related subjects including…

  • Creating a Windows 8 App
  • Orientation, Snapping, and Semantic Zoom
  • Searching and Sharing
  • The Windows Store
  • Application Bars and Media Capture
  • Settings and Preferences

And each of those subjects has separate, dedicated online sessions for C# and JavaScript.

Go register now to save your spot. I created a short URL to a search of all of the HOLOs being offered – http://aka.ms/holo.

I’m going to be presenting a number of the JavaScript courses, so I’ll see you online!

Tags: , , , , , , , , , , , , ,

Windows 8

Windows 8 2-day Camp - Content

by Jeremy Foster 6. August 2012 13:24

Thanks to all of the attendees of our Windows 8 2-day Camp and Hackathon last week!

It was an exhausting but excited couple of days and some attendees left with Samsung Series 7 slates, an XBox console, a bunch of XBox games, and more.

The Windows 8 content from day 1 will be available soon on Channel 9. Until then, I promised that I would upload the content from my session and the content from Bart De Smet’s session on Reactive Extensions.

My Content - Windows 8 Contracts

My session was an introduction to integrating the Windows 8 experience into your app by implementing the Windows 8 contracts. We looked at Search, Share, Settings, the FilePicker, and PlayTo. The code is extremely simple and intended to get you in the door. Let me know if you have any questions…

 CFContractDemos.zip (40.04 kb)

Bart De Smet’s Content – Reactive Extensions

Bart De Smet was a guest presenter on day 2 on the topic of Reactive Extensions. He presented mostly JavaScript examples, but the content below includes all of the .NET examples as well. If you’re not familiar with Reactive Extensions, I think you should get acquainted, and Bart’s slides are very well made and helpful in coming to an understanding.

Reactive Extensions.zip (6.53 mb)

Tags: , , , , , , , , , , , ,

C# | JavaScript | Windows 8

ReSharper 7 is Released

by Jeremy Foster 30. July 2012 19:51

I got a note in my inbox a few days ago saying that ReSharper 7 had been released. I was pretty surprised since I assumed they still had a ton of kinks to work out, but I’ve got it installed and am flying through some Windows 8 development and haven’t run into any glitches so far.

I’m kind of always excited about ReSharper, but here are a few reasons why version 7 is keeping my fire lit.

It supports VS 2012 and Windows 8 apps. It works with any of the last few versions of Visual Studio even when they’re installed side by side. It’s impressive that VS 2012 is not even released yet and R# is all over it. Not only does it work in VS 2012, but it support Asynchronous Solution Loading so it starts up faster, it uses the Preview tab, and it supports both the light and dark themes.

It gives you a hierarchical view of your CSS. Sweet. Look at this…

image

…and I noticed that it shows a little glyph next to CSS rules that are overriding the a rule elsewhere. That’s helpful.

Version 7 allows you to remove unused references from your entire solution at once.

Also, it adds a ton of support for JavaScript. It extends VS’s native IntelliSense for JavaScript. The ability for anyone to make IntelliSense for such an insanely dynamic language is beyond me for sure. It even added support for Jasmine unit testing.

There’s quite a bit more that it offers that I haven’t shared here, so check it out. This is one tool you should just throw down on, because it’s worth it. Another tool you should throw down on is CorelDRAW, but that’s another topic for another blog post.

Finally, I like their little catch phrase – More color to Visual Studio. If you’re familiar with the story around the color in Visual Studio between the developer preview and the release preview then you’ll have to laugh. And the “7” is made of little colored tiles. Groovy.

What's New in ReSharper 7

Tags:

ReSharper | Windows 8

A JavaScript Library for Everything

by Jeremy Foster 21. July 2012 08:29

Windows 8 is one of those environments that's just fun and expressive to develop in. So is JavaScript. The convergence of the two is just rockin'.

One of the great things about JavaScript is the enormous amount of code that other people have already written and put up on the web for your consumption. Do you want to wrap local storage (store.js), detect faces (liuliu), implement lightweight pubsub (minpubsub), or load JavaScript asynchronously (include.js)? You can do it by simply including a library in your Windows 8 project.

To find a library, you can always Bing (did you know that Bing results are preferred to Google 2:1 in blind taste tests?) for the functionality you need or you can try something here...

Tags: , , , , ,

JavaScript | Windows 8

BlockMover Sample from Jake and Sam

by Jeremy Foster 16. July 2012 09:06

Thanks to everyone that showed up to last Friday's session on JavaScript game development in Windows 8. I really appreciated the information and demonstration that Jake and Sam presented. I hope it was as helpful for you too.

I said I'd post the sample code that they used, and here it is. You will need to go to impactjs.com and purchase the library if you'd like to use this, but if you're actually making games then it's a small cost to consider.

Enjoy!

BlockMover.zip (346.49 kb)

Tags: , , , , , , , , , , , , , , ,

JavaScript | Windows 8

Random Tile Colors

by Jeremy Foster 12. July 2012 09:43

This tip is quite short and simple, but worth a post I think. If you want to colorize some tiles like the start screen does, here’s one simple way to do it.

var demos = [
    { key: "local", name: "Loading local data", group: "Data" },
    { key: "netflix", name: "Loading Netflix data", group: "Promises" },
    { key: "netflix10", name: "Loading Netflix data by 10", group: "Promises" },
    { key: "custom_promise", name: "Using custom promises", group: "Promises" },
    { key: "simple", name: "Simple data binding", group: "Data Binding" },
    { key: "properties", name: "Binding more properties", group: "Data Binding" },
    { key: "template", name: "Templates", group: "Data Binding" },
    { key: "share_simple", name: "Sharing text data", group: "Sharing"},
    { key: "datejs", name: "date.js", group: "Libraries"}
];
var colors = ["#0098ab", "#0070a9", "#d9532c", "#a400ac", "#009086", "#5838b4", "#ae193e", "#2c86ee", "#009c00"];
demos.forEach(function (i) {
    i.tileColor = colors[Math.floor(Math.random() * colors.length)];
});

This is some code from a project I have that showcases some simple code demos, so I have an array of each of these objects. Then I create a quick array of some of the color codes used on the start screen. Finally, I simply do a forEach on the demos array adding a new tileColor property to each which is a randomly selected color from the color array.

This ability to just throw another property onto an object without mucking with the class and affecting all of the other instances, is in my opinion one of the nicest things about a dynamic language like JavaScript, and I take advantage of it quite a lot.

Now to actually use this color for the tiles, it’s a simple matter of adding it to the databinding statement like this…

<div id="itemTemplate" data-win-control="WinJS.Binding.Template">
    <div class="tile" data-win-bind="onclick:click;style.backgroundColor:tileColor;">
        <h3 data-win-bind="innerText:name;"></h3>
    </div>
</div>

Notice the style.backgroundColor:tileColor term. That’s all it takes.

Tags:

Windows 8 | Design

How the WinJS.UI.ViewBox Actually Works

by Jeremy Foster 29. June 2012 20:58

I just learned something about the WinJS.UI.ViewBox and wanted to share.

I have known for some time what the ViewBox does, but I recently learned how it does it.

First, let me reiterate what the ViewBox does. I compared its functionality to the FlexBox in my When to Use ViewBoxes and FlexBoxes post. The ViewBox itself snaps to the size of the container it is in and resizes its child (without changing its aspect ratio) to fit.

How does it actually do that? It’s pretty cool. It does it with a CSS transform. If you create a ViewBox and then look at the DOM Explorer, you’ll see something like this…

<body>
  <div class="win-viewbox" data-win-control="WinJS.UI.ViewBox">
    <div style="transform-origin: left top; transform: translate(171px, 0px) scale(1);">
      <p>Content goes here</p>
    </div>
  </div>
</body>

Notice a few things about this generated code. First, notice the inline style attribute on the second div. It’s not on the ViewBox itself, but rather on its first (and only) child. The values indicate that the div should be translated 171px from the left. The ViewBox has calculated the shape of my screen (1366 x 768 in this case) and the shape of that only child div (1024 x 768), and determined that it needs to scoot over to the right 171 pixels in order to center it within the screen.

Now I snap my app and then check out the DOM Explorer again and here’s what I have…

<body>
  <div class="win-viewbox" data-win-control="WinJS.UI.ViewBox">
    <div style="transform-origin: left top; transform: translate(0px, 264px) scale(0.3125);">
      <p>Content goes here</p>
    </div>
  </div>
</body>

So there you have it. In order to arrange the child div in the center of the snap view, it’s going to need to translate (move) it down 264 pixels and shrink it to 31% (0.3125) of its original size.

I could have continued not knowing the how of this, but I’ll sleep better tonight knowing it.

Tags:

Windows 8

Feed Subscribe